Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drops validation from belongs_to relations in Rails 5 #94

Open
mvz opened this issue Dec 18, 2016 · 5 comments
Open

Drops validation from belongs_to relations in Rails 5 #94

mvz opened this issue Dec 18, 2016 · 5 comments

Comments

@mvz
Copy link

mvz commented Dec 18, 2016

In Rails 5, a belongs_to relation is automatically validated as required. Adding activerecord-session_store to the Gemfile of a Rails 5 project removes this automatic validation.

This can be reproduced very easily by creating a Rails 5 project with two related models:

rails new quuz
cd quuz
rails g model foo name:string
rails g model bar name:string foo:references

In the rails console or a test, it can now easily be verified that a Bar object is invalid without a Foo object:

Bar.new.valid?                # => false
Bar.new(foo: Foo.new).valid?  # => true

If activerecord-session_store is added to the Gemfile like so,

gem 'activerecord-session_store'

the results are different:

Bar.new.valid?                # => true
Bar.new(foo: Foo.new).valid?  # => true
@mvz
Copy link
Author

mvz commented Dec 18, 2016

I've done some debugging, and this seems to have to do with ActiveRecord::Base being loaded before config/initializers/new_framework_defaults.rb is loaded. If I change the following line in that file:

Rails.application.config.active_record.belongs_to_required_by_default = true

to:

ActiveRecord::Base.belongs_to_required_by_default = true

then the validation is restored.

@mauro-ni
Copy link

Hope to have this fixed soon.

@jrochkind
Copy link

Is this still an issue? Seems kinda disastrous.

@mvz
Copy link
Author

mvz commented Oct 5, 2018

I just tried and it no longer occurs with Rails 5.2 and activerecord-session_store 1.1.1.

@mvz
Copy link
Author

mvz commented Oct 5, 2018

It also does not occur with Rails 5.1.6 and activerecord-session_store 1.1.1.

However, it does still happens with Rails 5.0.7 and activerecord-session_store 1.1.1.

I think projects that have migrated to Rails 5.0 and then to 5.1 and 5.2 may also be affected if they haven't removed config/initializers/new_framework_defaults.rb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants