验证关联模型属性的最简单的方法是什么?
Item
belongs_to :user
validates_presence_of :user
# AND the "is_photographer" column for that user must be true
User
has_many :items
# can be a regular user or a photographer
答案 0 :(得分:5)
validate :user_is_photographer, :if => :user
def user_is_photographer
errors.add(:user, "should be a photographer") unless user.is_photographer
end