thoughtbot / thoughtbot/factory_bot

Associations with Rails Engine lead to ActiveRecord::RecordInvalid

Open
#1,584 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Ruby
Stars
8.2k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

Description

I use simple 1:n associations with models residing in a Rails Engine. The main app includes this engine directly under /engines.
However, the validation fails for belongs_to associations when I use implicit or explicit associations. I even tried out before(:create) and after(:build) hooks. Only initialize_with works, which is kind of limiting.
My test just executes FactoryBot.build(:thing) (see factories_test.rb). FactoryBot.lint results in the same error.

Error:
ActiveRecord::RecordInvalid: Validation failed: [MODEL] must exist

My overall setup is a bit more complex. However, this should not influence this bug, as these models / factories use the same database and are integrated into the same Engine. All other parts of this application work - including model tests with manually constructing the models. But FactoryBot seems to ignore my association and just uses nil for it.

Any suggestions? Is there some restriction, when using models from Rails Engines?

Reproduction Steps

I could not reproduce this bug with your reproduction script - as it seems to be connected to the Engine.

engines/some-engine/test/factories/thing_factory.rb in Rails Engine

FactoryBot.define do
  factory :thing, class: "Thing" do
    # simple association does not work.
    association :container, factory: :container

    # before(:create) and after(:build) don't work.
    after(:build) do | model |
      container = create(:container)
    end

    # this does not work, too.
    container { FactoryBot.create(:container) } 

    # only initialize_with works.
    initialize_with do
      container = create(:container)
      new(attributes.merge(container: container))
    end
  end
end

engines/some-engine/test/factories/container_factory.rb in Rails Engine

FactoryBot.define do
  factory :container, class: "Container" do
   ...
  end
end

engines/some-engine/app/models/thing.rb in Rails Engine

# table with container_id

class Thing < ApplicationRecord
  belongs_to :container
end

engines/some-engine/app/models/container.rb in Rails Engine

class Container < ApplicationRecord
  has_many :things
end

test/factories_test.rb in main app

FactoryBot.factories.each do |factory|
  method_name = "test_#{factory.name}"
  define_method(method_name) do
    instance = FactoryBot.build(factory.name)
    assert instance.valid?, "invalid factory: #{factory.name}, error messages: #{instance.errors.messages.inspect}"
  end
end

Resulting Error:
ActiveRecord::RecordInvalid: Validation failed: Container must exist

Expected behavior

FactoryBot.build(:thing) should work with associations.

Actual behavior

Error when executing FactoryBot.lint:
ActiveRecord::RecordInvalid: Validation failed: Container must exist

System configuration

factory_bot version: 6.2.0 (factory_bot_rails)
rails version: 7.0.4.2
ruby version: 3.2.2
Test Framework: Minitest

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with engines/some-engine/test/factories/thing_factory.rb and container_factory.rb, then inspect the Thing and Container models and run test/factories_test.rb or FactoryBot.lint. The supplied reproduction script did not reproduce the issue, so first establish a minimal Rails Engine case where FactoryBot.build(:thing) leaves Container unset. Done means the association builds successfully without RecordInvalid.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
backend, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.