Here is my code for the Photo model class:
class Photo < ActiveRecord::Base
belongs_to :user
belongs_to :organization
validate :user_id_or_organization_id_cant_be_blank
...
def user_id_or_organization_id_cant_be_blank
if !:user_id? and !:organization_id?
errors.add(:user_id, "no user")
errors.add(:organisation_id, "no organization")
end
end
The problem is in the validation. It doesn’t work, and i don’t understand why.
I can create a photo with no user_id or organization_id which is not supposed to happen.
Please explain to me what i’m doing wrong?
You’d rather do: