Skip to content

Commit

Permalink
Wrap ActiveRecord configuration into initializer
Browse files Browse the repository at this point in the history
`Rails.application` is not available when the railties are collected,
so we move the ActiveRecord configuration into the initializer phase.

```
NoMethodError: undefined method `config' for nil:NilClass (NoMethodError)
      application.config
                 ^^^^^^^
/project/app/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails.rb:47:in `configuration'
/project/app/vendor/bundle/ruby/3.2.0/gems/factory_bot_rails-6.4.0/lib/factory_bot_rails/railtie.rb:25:in `block in <class:Railtie>'
```
  • Loading branch information
leoarnold committed Nov 17, 2023
1 parent 3346305 commit 8205851
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/factory_bot_rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ class Railtie < Rails::Railtie
FactoryBot.definition_file_paths = definition_file_paths
end

ActiveSupport.on_load :active_record do
config = Rails.configuration.factory_bot
initializer "factory_bot.reject_primary_key_attributes" do
ActiveSupport.on_load :active_record do
config = Rails.configuration.factory_bot

if config.reject_primary_key_attributes
require "factory_bot_rails/factory_validator/active_record_validator"
if config.reject_primary_key_attributes
require "factory_bot_rails/factory_validator/active_record_validator"

config.validator.add_validator FactoryValidator::ActiveRecordValidator.new
config.validator.add_validator FactoryValidator::ActiveRecordValidator.new
end
end
end

Expand Down

0 comments on commit 8205851

Please sign in to comment.