Reputation: 3634
What's the simplest railsy way to validate an attribute of an associated model?
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
Upvotes: 1
Views: 50
Reputation: 222398
validate :user_is_photographer, :if => :user
def user_is_photographer
errors.add(:user, "should be a photographer") unless user.is_photographer
end
Upvotes: 5