Last Updated: February 25, 2016
·
534
· ka8725

Rule to use presence belongs_to association

Do you know what to validate for presence if you have belongs_to association: association name or foreign key?

I mean what do you use?:

belongs_to :account
validates :account_id, :presence => true

Or:

belongs_to :account
validates :account, :presence => true

Use second variant!

This is an explanation why: http://railsguides.net/2013/07/11/belongs-to-and-presence-validation-rule1/