Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Belongs_to presence in Rails 5 not working

To my knowledge, the new default in Rails 5 requires belongs_to associations to be present. I made a model with this association, but the problem is I don't get presence validation error when the associated field is empty. Instead I get a database Null Validation error since I set the _id column not to be null. (PG::NotNullViolation because I use Postgres)

Is this behaviour normal? I mean shouldn't I get the rails error only?

BTW, when I add presence validation for the field, it works as I expected.

like image 973
Arvinje Avatar asked Jul 02 '16 06:07

Arvinje


People also ask

What is the difference between Has_one and Belongs_to?

They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.

What is Belongs_to in Ruby on Rails?

2.1 The belongs_to AssociationA belongs_to association sets up a connection with another model, such that each instance of the declaring model "belongs to" one instance of the other model.

What does belongs_ to do?

belongs_to means that the foreign key is in the table for this class. So belongs_to can ONLY go in the class that holds the foreign key. has_one means that there is a foreign key in another table that references this class. So has_one can ONLY go in a class that is referenced by a column in another table.

What is optional true in rails?

What is optional true in rails? If you set the :optional option to true, then the presence of the associated object won't be validated. By default, this option is set to false . otherwise it will be required associated object.29-Aug-2020.


2 Answers

According to the issue re weird behaviour of config belongs_to_required_by_default, it seems like one of your other gems intervenes in ActiveRecord::Base and causes the bug.

One of workarounds to the issue is to move the line

config.active_record.belongs_to_required_by_default = true

from initializers directly into application.rb.

This worked for me smoothly.

like image 127
Oleg Afanasyev Avatar answered Oct 19 '22 07:10

Oleg Afanasyev


New Rails 5 applications come with a new initializer in

config/initializers/active_record_belongs_to_required_by_default.rb

If you upgraded a Rails 4 application or created your application with a beta version of Rails 5, then that file might be missing.

The configuration in that file enables the feature in question:

# Be sure to restart your server when you modify this file.

# Require `belongs_to` associations by default. This is a new Rails 5.0
# default, so it is introduced as a configuration option to ensure that apps
# made on earlier versions of Rails are not affected when upgrading.
Rails.application.config.active_record.belongs_to_required_by_default = true

Please check how belongs_to_required_by_default is configured in your application.

like image 3
spickermann Avatar answered Oct 19 '22 06:10

spickermann