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

Rails 5 belongs_to_required_by_default not working #27844

Closed
elquimista opened this issue Jan 30, 2017 · 8 comments
Closed

Rails 5 belongs_to_required_by_default not working #27844

elquimista opened this issue Jan 30, 2017 · 8 comments

Comments

@elquimista
Copy link

Steps to reproduce

begin
  require 'bundler/inline'
rescue LoadError => e
  $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
  raise e
end

gemfile(true) do
  source 'https://rubygems.org'
  gem 'rails', '5.0.1'
  gem 'sqlite3'
end

require 'active_record'
require 'active_record/railtie'
require 'minitest/autorun'
require 'logger'
require 'irb'

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

class TestApp < Rails::Application
  config.root = File.dirname(__FILE__)
  config.active_record.belongs_to_required_by_default = true
end

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :teams, force: :cascade do |t|
  end

  create_table :subscriptions, force: :cascade do |t|
    t.references :team, foreign_key: true
  end
end

class Team < ActiveRecord::Base
  has_one :subscription, dependent: :destroy
end

class Subscription < ActiveRecord::Base
  belongs_to :team
end

class BugTest < Minitest::Test
  def test_association_stuff
    s = Subscription.new(team: nil)
    s.validate
    assert_includes s.errors.details[:team], { error: :blank }
  end
end

Expected behavior

Test should have passed.

Actual behavior

Test fails.

System configuration

Rails version: 5.0.1

Ruby version: 2.4.0p0

@yskkin
Copy link
Contributor

yskkin commented Jan 31, 2017

http://stackoverflow.com/a/41367484/2357117
ActiveRecord::Base.belongs_to_required_by_default = true works.

I think TestApp does not take care of config in this case.

@rafaelfranca
Copy link
Member

The problem is that ActiveRecord::Base is being loaded before the application is initialized so the config is not being applied. The same is happening in your application because some gem or monkey patch is loading ActiveRecord::Base before the initialization is in place.

@elquimista
Copy link
Author

elquimista commented Jan 31, 2017

@rafaelfranca I retried test after moving TestApp class declaration up to right above require 'active_record' statement, and now it complains active_record is undefined method for Rails.application.config.

@elquimista
Copy link
Author

Any suggestions to make the above test pass?

slim-rails
devise
awesome_nested_set
apartment
jwt
public_activity
stripe
js-routes

These are the additional gems I use for my app (I didn't mention gems for testing, e.g. capybara, poltergeist etc, as I believe they are not the culprit.)
Which one do you think is monkeypatching AR and causing this issue?

@rafaelfranca
Copy link
Member

I'd take a look at apartment. The problem is not require 'active_record but the ActiveRecord::Base call. If apartment does call ActiveRecord::Base at boot time this problem will happen.

@elquimista
Copy link
Author

Ok thanks.

@elquimista
Copy link
Author

I did a test today and as you can see, TestApp class is declared before ActiveRecord::Base call. But test still fails - what is wrong with the ruby code above?

@george-carlin
Copy link
Contributor

If anyone else is having this same issue, I "fixed" it by removing the active_record_belongs_to_required_by_default.rb initializer and adding the line self.belongs_to_required_by_default = true to my ApplicationRecord.

Really, this fixes the symptom rather than the problem - I'm not sure what's causing the initializer to not be loaded probably; it's probably one of my gems. But the above hack is good enough for now.

jsugarman added a commit to ministryofjustice/Claim-for-Crown-Court-Defence that referenced this issue Jul 19, 2021
NOTE: this setting is not currently taking effect since it appears
that govuk_notify_rails is causing early rails loading and
causing it to be "lost".

This is a workaround for issue rails/rails#39855 (comment)
suggested by this issue comment rails/rails#37030 (comment)
and this activerecord specific issue rails/rails#27844
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants