我有一个专辑模型和Track模型。我想确保他们所属专辑中每首曲目的名称是唯一的。我尝试在Track
模型中使用此验证
validates :name, presence: true, uniqueness: true, scope: :album_id
但得到错误:Unknown validator: 'ScopeValidator'
我做错了什么?
class Album < ActiveRecord::Base
has_many :tracks
accepts_nested_attributes_for :tracks, :reject_if => :all_blank, :allow_destroy => true
validates :name, presence: true, uniqueness: true
end
class Track < ActiveRecord::Base
belongs_to :album
validates :name, presence: true, uniqueness: true, scope: :album_id
end
答案 0 :(得分:2)
您需要确定唯一性的范围,而不是将其作为单独的参数。
class Track < ActiveRecord::Base
belongs_to :album
validates :name, presence: true, uniqueness: { scope: :album_id }
end