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 

validates_presence_of


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -