我有以下三种模式:
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
数据库表上检查它。