Quantcast
Viewing all articles
Browse latest Browse all 21

Fixing Association Type Mismatch error in Rails and FactoryGirl

I was getting a big ugly error running FactoryGirl in my rails tests:

ActiveRecord::AssociationTypeMismatch: Organization(#2159382480) expected, got FactoryGirl::Declaration::Implicit(#2174473920)

I was able to trace the error to code where I was building up Associations:

factory :organization do
  name 'Org Name'

  factory :organization_graph do
    after_create do |organization|
      FactoryGirl.create_list(:device, 5, organization: organization)
      FactoryGirl.create_list(:project_graph, 5, organization: organiaztion)
    end
  end
end

The error occurred whenever I had the :project_graph creation occurring. Removing the line and the code would run fine.

All the Googling for a solution brought back weird issues with rspec, cucumber, etc. I wasn’t using any of those. The issue was actually really simple. I misspelled organization. Take a look at the value being passed in via the organization symbol:

FactoryGirl.create_list(:project_graph, 5, organization: organiaztion)

Yup, I spelt it organiaztion not organization. FactoryGirl is trying to integrate the misspelling as part of it’s DSL which is why I get such an obscure error. Fix the spelling – fix the error.


Viewing all articles
Browse latest Browse all 21

Trending Articles