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 railtie included in #419 breaks compatibility with some gems "undefined method `config' for nil:NilClass" Factory Bot > 6.2.0 #426

Closed
2 of 3 tasks
tagliala opened this issue Sep 24, 2023 · 5 comments
Labels

Comments

@tagliala
Copy link

tagliala commented Sep 24, 2023

Hello,

I was testing the main branch and the application didn't boot because of this error:

~/.rvm/gems/ruby-3.2.2/gems/railties-7.0.8/lib/rails.rb:47:in `configuration': undefined method `config' for nil:NilClass (NoMethodError)

      application.config
                 ^^^^^^^
	from ~/.rvm/gems/ruby-3.2.2/bundler/gems/factory_bot_rails-b965bb8f6430/lib/factory_bot_rails/railtie.rb:25:in `block in <class:Railtie>'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:95:in `class_eval'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:95:in `block in execute_hook'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:85:in `with_execution_control'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:90:in `execute_hook'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:76:in `block in run_load_hooks'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:75:in `each'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:75:in `run_load_hooks'
	from ~/.rvm/gems/ruby-3.2.2/gems/activerecord-7.0.8/lib/active_record/base.rb:333:in `<module:ActiveRecord>'
	from ~/.rvm/gems/ruby-3.2.2/gems/activerecord-7.0.8/lib/active_record/base.rb:15:in `<main>'

I think that this is not an issue with Factory Bot Rails, but it is a problem with third-party gems or implementations that does not rely on lazy hooks or autoload to add new modules to ActiveRecord::Base.

I opened this here because updating factory bot triggers this behavior and may affect other users that may search here for a solution

Reproduction Steps

  1. Add database_cleaner-active_record to Gemfile (or call ActiveRecord::Base before the module block of config/application.rb)
  2. run rails c

Expected behavior

Console opens

Actual behavior

Console does not open and raises an error

System configuration

factory_bot_rails version: main
factory_bot version: 6.3.0
rails version: 7.0.8
ruby version: 3.2.2/3.0.4

Bisected to: b965bb8

Affected gems

@tagliala tagliala added the bug label Sep 24, 2023
@tagliala tagliala changed the title The railtie included in #419 breaks compatibility with some gems "undefined method `config' for nil:NilClass" The railtie included in #419 breaks compatibility with some gems "undefined method `config' for nil:NilClass" Factory Bot Rails 6.4.0 Sep 24, 2023
tagliala added a commit to tagliala/amoeba that referenced this issue Sep 24, 2023
Extend ActiveRecord::Base functionality only after ActiveRecord has been
fully loaded.


Ref: thoughtbot/factory_bot_rails#426
tagliala added a commit to tagliala/amoeba that referenced this issue Sep 24, 2023
Extend ActiveRecord::Base functionality only after ActiveRecord has been
fully loaded.

Ref: thoughtbot/factory_bot_rails#426
@tagliala tagliala changed the title The railtie included in #419 breaks compatibility with some gems "undefined method `config' for nil:NilClass" Factory Bot Rails 6.4.0 The railtie included in #419 breaks compatibility with some gems "undefined method `config' for nil:NilClass" Factory Bot > 6.2.0 Sep 25, 2023
tagliala added a commit to tagliala/amoeba that referenced this issue Sep 29, 2023
Extend ActiveRecord::Base functionality only after ActiveRecord has been
fully loaded.

Ref: thoughtbot/factory_bot_rails#426
tagliala added a commit to ifad/chronomodel that referenced this issue Oct 8, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Ref: thoughtbot/factory_bot_rails#426


Close #231
tagliala added a commit to ifad/chronomodel that referenced this issue Oct 16, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Ref: thoughtbot/factory_bot_rails#426


Close #231
tagliala added a commit to ifad/chronomodel that referenced this issue Oct 19, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Ref: thoughtbot/factory_bot_rails#426


Close #231
tagliala added a commit to ifad/chronomodel that referenced this issue Oct 19, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Ref: thoughtbot/factory_bot_rails#426


Close #231
tagliala added a commit to ifad/chronomodel that referenced this issue Oct 19, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Ref: thoughtbot/factory_bot_rails#426


Close #231
tagliala added a commit to ifad/chronomodel that referenced this issue Oct 19, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Ref: thoughtbot/factory_bot_rails#426


Close #231
tagliala added a commit to ifad/chronomodel that referenced this issue Oct 20, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Ref: thoughtbot/factory_bot_rails#426


Close #231
tagliala added a commit to ifad/chronomodel that referenced this issue Nov 1, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Ref: thoughtbot/factory_bot_rails#426


Close #231
tagliala added a commit to ifad/chronomodel that referenced this issue Nov 4, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Ref: thoughtbot/factory_bot_rails#426

Close #231
tagliala added a commit to ifad/chronomodel that referenced this issue Nov 4, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Also:
- Require Railtie as suggested by APIs
- Patch DBConsole only if needed
- Move application configuration check in an after initialize lazy load hook
  to be sure that `Rails.application` is available at the moment of the check

Ref:
- thoughtbot/factory_bot_rails#426
- https://edgeapi.rubyonrails.org/classes/Rails/Railtie.html#class-Rails::Railtie-label-Creating+a+Railtie

Close #231
tagliala added a commit to ifad/chronomodel that referenced this issue Nov 4, 2023
Extend `ActiveRecord::Base` functionality only after `ActiveRecord` has
been fully loaded.

Also:
- Require Railtie as suggested by APIs
- Patch DBConsole only if needed
- Move application configuration check in an after initialize lazy load hook
  to be sure that `Rails.application` is available at the moment of the check

Ref:
- thoughtbot/factory_bot_rails#426
- https://edgeapi.rubyonrails.org/classes/Rails/Railtie.html#class-Rails::Railtie-label-Creating+a+Railtie

Close #231
@tagliala
Copy link
Author

tagliala commented Nov 18, 2023

Reopen because of #432

@tagliala tagliala reopened this Nov 18, 2023
@tagliala
Copy link
Author

Closing again in favour of #433

@marclennox
Copy link

I'm having the same issue, which thanks to this thread, I was able to isolate down to the consul gem that we're using (https://github.com/makandra/consul).

I tried forking this gem and making the prescribed change (makandra/consul@04aa667)

Unfortunately, even using this forked version of the gem, I'm getting the same error for anything greater than version 6.2 of the factorbot rails gem.

Is there something else I'm missing?

Thank you

@tagliala
Copy link
Author

Maybe this is related to #432

@marclennox
Copy link

Thanks @tagliala , this merge request fixes the issue. Until this gets merged, I'll just use a forked copy of factorybot_rails with this change applied.

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

No branches or pull requests

2 participants