Validation failed Class must exist Validation failed Class must exist ruby-on-rails ruby-on-rails

Validation failed Class must exist


Try the following:

belongs_to :city, optional: true

According to the new docs:

4.1.2.11 :optional

If you set the :optional option to true, then the presence of theassociated object won't be validated. By default, this option is setto false.


This comes a bit late but this is how to turn off this by default in rails 5:

config/initializers/new_framework_defaults.rb

Rails.application.config.active_record.belongs_to_required_by_default = false

In case you don't want to add optional: true to all your belongs_to.

I hope this helps!


You need to add the following to the end of the belongs_to relationship statement:

optional: true

It is possible to set this on a global level so that it works in the same way as older versions of rails, but I would recommend taking the time to manually add it to the relationships that really need it as this will cause less pain in the future.