我有以下三个模型:
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