我正在尝试验证video_id相对于session_id的唯一性。我不知道我做错了什么。这是我的模型代码。
class SessionWatchHistory < ActiveRecord::Base
validates_presence_of :session_id, :on => :create, :message => "can't be blank"
validates_presence_of :video_id, :on => :create, :message => "can't be blank"
belongs_to :video, :class_name => "Video", :foreign_key => "video_id"
scope :per_session, lambda{ |s| where("session_id = :session", :session => s)}
validates_uniqueness_of :video_id, :scope => [:per_session]
end
答案 0 :(得分:5)
如果要使用其他字段验证字段的唯一性,则需要将其他字段放在范围中。所以相对于session_id将是:
validates_uniqueness_of :video_id, :scope => [:session_id]