Scott's Recipes Logo

The Dreaded FactoryBot Setup Error


Please note that all opinions are that of the author.


Last Updated On: 2025-11-26 06:51:47 -0500

Of all the errors I find and deal with in any of my Rails coding, I hate this one the most:

❯ bin/rails test test/controllers/event_types_controller_test.rb:71
Source locally installed gems is ignoring #<Bundler::StubSpecification name=nkf version=0.2.0 platform=ruby> because it is missing extensions
Source locally installed gems is ignoring #<Bundler::StubSpecification name=debug version=1.10.0 platform=ruby> because it is missing extensions
DEBUG RAILS_MASTER_KEY: nil
Running 21 tests in a single process (parallelization threshold is 50)
Run options: --seed 31827

# Running:

DEPRECATION WARNING: Mapping a route with multiple paths is deprecated and will be removed in Rails 8.1. Please use multiple method calls instead. (called from block (3 levels) in <main> at /Users/sjohnson/Sync/coding/rails8_take20/hub/config/routes.rb:102)
DEPRECATION WARNING: Mapping a route with multiple paths is deprecated and will be removed in Rails 8.1. Please use multiple method calls instead. (called from block (3 levels) in <main> at /Users/sjohnson/Sync/coding/rails8_take20/hub/config/routes.rb:116)
E

Error:
EventTypesControllerTest#test_should_create_domain_if_logged_in:
NoMethodError: undefined method 'role_id' for an instance of EventType
    test/controllers/event_types_controller_test.rb:5:in 'block in <class:EventTypesControllerTest>'


bin/rails test test/controllers/event_types_controller_test.rb:71



Finished in 0.464402s, 2.1533 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

This error:

NoMethodError: undefined method 'role_id' for an instance of EventType
    test/controllers/event_types_controller_test.rb:5:in 'block in <class:EventTypesControllerTest>'

is god fucking awful to troubleshoot. The reason is that this is the factory for event_type:

factory :event_type do
  sequence(:name) { |n| "Event Type #{n}" }
  sequence(:fid)  { |n| "ET#{n.to_s.rjust(4, '0')}" }

  association :organization
  association :user

  youtube_url { "https://www.youtube.com/watch?v=dQw4w9WgXcQ" }
  body         { "This is a sample description of the event type." }
  active       { true }

  created_at { Time.current }
  updated_at { Time.current }
end

As you can see there is not one smell of role_id in this. What this means is that somewhere in the codebase there is some kind of association that is being walked to a different factory and that factory is, well, fucked up. And it always means the same awful fucking process – fix fucking everything.

class EventType < ApplicationRecord
  belongs_to :organization, optional: true
  belongs_to :user
  
  validates_uniqueness_of :fid
  validates :name, presence: true
  validates :fid, presence: true
  validates :user_id, presence: true
  
  validates :role_id, uniqueness: { scope: :user_id }
end