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

The required validation which comes from 'belongs_to' in Rails 5 doesn't work #375

Open
kyamaguchi opened this issue Sep 5, 2017 · 1 comment

Comments

@kyamaguchi
Copy link

Tested on the sample app.
https://github.com/kyamaguchi/required_validation_from_belongs_to

Rails 5 requires relation with belongs_to by default.
rails/rails#18233

But it doesn't work after adding 'audited'.

$ rails console

> User.new(name: 'Foo').valid?
 => true

Before adding 'audited' gem,

> Company.create(name: 'Test')
> User.new(name: 'Foo').valid?
 => false
> User.new(name: 'Foo', company: Company.first).valid?
 => true

I tested on the different versions of Rails on branches on the sample app and it works fine with rails 5.1.
It doesn't work with rails '>= 5.0.0, ~> 5.0.0'.


Simple solutions are

  1. Upgrading Rails to 5.1

  2. Add required: true option

class User < ApplicationRecord
  belongs_to :company, required: false
@brendon
Copy link

brendon commented Sep 3, 2019

From memory this was to do with ActiveRecord being initialised way before the Rails Default initialiser file is run (due to a particular gem touching it early on). The solution is to configure it earlier in the boot process, or as you say, just keep on upgrading through to Rails 5.1.

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

2 participants