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

ActiveRecord::AssociationTypeMismatch on belongs_to #1029

Closed
Quintasan opened this issue Jul 26, 2017 · 9 comments
Closed

ActiveRecord::AssociationTypeMismatch on belongs_to #1029

Quintasan opened this issue Jul 26, 2017 · 9 comments

Comments

@Quintasan
Copy link

Hello,

This was asked a million times but the only working solution I found was to set config.cache_classes = false in config/environments/development.rb which is less than optimal for development as one could guess. I am also not using Spring.

What I basically do is:

u = FactoryGirl.create(:user)
o = FactoryGirl.create(:organization, user: u)
(...)
ActiveRecord::AssociationTypeMismatch: Country(#79985380) expected, got #<Country id: 9, name: "Country #9", created_at: "2017-07-26 11:33:04", updated_at: "2017-07-26 11:33:04"> which is an instance of Country(#62001400)

Is the only way to enable the class caching or am I doing something wrong?

organization.rb

class Organization < ApplicationRecord
(...)
  has_many :users
  belongs_to :user
  belongs_to :country
(...)
end

spec/factories/organizations.rb

# frozen_string_literal: true

FactoryGirl.define do
  factory :organization do
    sequence(:name) { |n| "Organization ##{n}" }
    # https://4programmers.net/Forum/Inne/191628-generowanie_numeru_nip
    sequence(:nip) do |n|
      srand(n)
      sum = 10
      nip = []
      while sum == 10
        nip = Array.new(9) { rand(10) }
        sum = nip.zip([6, 5, 7, 2, 3, 4, 5, 6, 7]).map{ |a| a.inject(&:*) }.inject(&:+) % 11
      end
      (nip << sum).join
    end
    address "Przykładowa 2/8"
    postcode "50-123"
    city "Wrocław"
    status :created
    association :country
    association :user
  end
end

country.rb

# frozen_string_literal: true

class Country < ApplicationRecord
  validates :name, presence: true, uniqueness: true
  has_many :users
  has_many :projects
end

spec/factories/countries.rb

FactoryGirl.define do
  factory :country do
    sequence(:name) { |n| "Country ##{n}" }
  end
end
@gnclmorais
Copy link

Hey @Quintasan! A few suggestions/ideas:

  • Have you tried setting your factories with singular filenames? Like factories/organization.rb, factories/country.rb and so on?
  • Have you tried simplifying the association within the factory (as per example)?
    factory :post do
      # ...
      author
    end
  • Have you tried explicitly declaring your factory (as per example)?
    factory :post do
      # ...
      association :author, factory: :user
    end

@travisofthenorth
Copy link

travisofthenorth commented Feb 13, 2019

@gnclmorais this is happening to me and a few members of my team when we specifically change code and then run tests again. Without changing app code, you can run a spec over and over with no errors. A single code change and this error occurs until you spring stop (we're using spring).

Singular filenames, explicitly declaring factories...none of it seems to work. Any ideas? We are on rails 5.2.2 and factory_bot/factory_bot_rails 5.0.0. In test.rb, we have cache_classes = true

@composerinteralia
Copy link
Collaborator

composerinteralia commented Feb 13, 2019

@travisofthenorth was this happening before you upgraded to 5.0.0, or is this new for you (I ask because we did make some changes to the way factory_bot_rails handles loading file definitions in 5.0.0)?

@composerinteralia
Copy link
Collaborator

I should also mention that some version of this has been happening for a long time and and we have a documented workaround: https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#rails-preloaders-and-rspec

@travisofthenorth
Copy link

@composerinteralia it only started happening after the upgrade.

Wow, I did not know about this workaround. It seems to have fixed the problem for us! Thank you so much!

@composerinteralia
Copy link
Collaborator

composerinteralia commented Apr 5, 2019

This should be fixed with Fixed with thoughtbot/factory_bot_rails#330. I will try to get a factory_bot_rails release out next week.

@hwhelchel
Copy link

@composerinteralia I'm still getting this error in development rails console (pry). I'm on rails 6 and 5.0.2 factory bot. Has a release been cut with this fix yet?

@composerinteralia
Copy link
Collaborator

composerinteralia commented Aug 20, 2019

thoughtbot/factory_bot_rails#329 went out in factory_bot_rails 5.0.2. You might be experiencing a different problem with a similar symptom. Are you able to share a sample application that demonstrates the problem?

@hwhelchel
Copy link

@composerinteralia yes happy to do so and will open a new issue thanks

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

No branches or pull requests

5 participants