Toll free (US) : +1 (888) 838-3532 | NYC: +1 (646) 491-6866

Rails 4:
Let’s assume we have two models Movie and Actor. Actor has many movies and Movie belongs to Actor. In most of the cases, the developers used to validate the foreign key actor_id is present or not. In this case, it will not validate actor model as to whether the entered actor id exists or not. It is just like attribute validation which always validates that the fields should be non-empty when submitting the form.

Validation with foreign key:

The scenario above, validates actor id presence or not like normal attribute presence validator.

We can still save the movie record without valid actor.

With associations


In this case, it will always validate whether the entered actor exists or not. In case of an invalid actor it throws error to you. This is the best practise to make your associations. It always checks for the associated object exists or not.


From rails5 these validations were added by default. It validates association object should be present when you define belongs_to associations.

Release notes will now trigger a validation error by default if the association is not present.)

We can opt out of this feature completely from the application by setting config opton in initializer file.

This initializer file is present in rails5 application only. Need to add this initializer file manually when you migrate from older version of your rails application and make necessary changes.

We can not create any comment record without an associated record.

We can opt out this default behaviour by setting

RailsCarma has been offering Ruby on Rails development services for over past 8 years. Our developers are well versed with executing all kind of Ruby on Rails web development projects and enhancing the features of your existing Rails applications. Contact us to know more about our development skills and the projects we have worked on.