验证Rails中的委托方法

时间:2013-06-03 02:21:57

标签: ruby-on-rails ruby-on-rails-3

我有以下三种模式:

class Team < ActiveRecord::Model
  has_many :teams_players
  has_many :players, :through => :teams_players
end

class TeamsPlayer < ActiveRecord::Model
  belongs_to :team
  belongs_to :player

  delegate :position, :to => :player
end

class Player < ActiveRecord::Model
  has_many :teams_players
  has_many :teams, :through => :teams_players

  # the database attribute 'position' exists on Player
end

一个团队可以有多个玩家,玩家可以属于多个团队。但是,一支球队只能有一名球员位置'踢球'。

如何创建此独特验证?

我尝试在TeamsPlayer上添加验证,如下所示:

validates :position, :uniqueness => { :scope => {:team_id} }

但在验证期间,rails运行查询,检查type数据库表上的TeamsPlayer列。相反,我希望它能够识别它是一个委托方法,并在Player数据库表上检查它。

1 个答案:

答案 0 :(得分:0)

这不是Player的验证,Team上的custom validation