ruby on rails - Validate existence of submodel in 'has_one' relation -
i have 2 models, 1 post
, genre
. i'm creating new post given genre_id
. best way of validating if genre given id exists? i'm validating presence of genre_id, it's not enough.
validates :genre_id, presence: true
i know can check whether genre exists in controller, prefer have in post validator object.
you can explicitly tell rails validate genre
association , not attribute, genre_id
with:
has_one :genre validates_presence_of :genre
Comments
Post a Comment