-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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 same class but different object_ids #1330
Comments
Key lines that cause the error to trigger are:
Comment either of those out and it runs cleanly. So maybe when ActiveStorage analyzes the profile photo in the inline job queue a new |
Eep! I was hoping I had seen the last of that error. It comes up every so often, and we have a workaround in the README. I appreciate your detailed report and reproduction instructions. This seems like a fun one to dig into. I won't have time right away, so feel free to keep digging if you have any further ideas about why this might be happening. I was hoping we might get lucky and thoughtbot/factory_bot_rails#343 would fix it, but alas it did not help matters. I also wondered if the caching of the class in https://github.com/thoughtbot/factory_bot/blob/master/lib/factory_bot/factory.rb#L22-L28 might be causing problems, but I didn't have much luck fiddling with that either. |
@composerinteralia thanks for looking into this. In development if I run: rails c
> FactoryBot.reload
> FactoryBot.create(:story) I still get an error. Hope that shines some helpful light on the problem. Turning off When you can revisit, let me know what you find! |
Some, but not all, of us are getting the account = FactoryBot.create(:account)
product = Product.create!(account: account, ....) I noticed while the class has the same name, it is a different object: account.class.object_id
=> 69890016840780
Account.object_id
=> 69889857397040
account.class == Account
=> false
ObjectSpace._id2ref(account.class.object_id) == ObjectSpace._id2ref(Account.object_id)
=> false Doing Update: disabling spring fixes this, e.g. |
I finally did some deeper digging into this, and it turns out it is not a factory_bot problem after all. Although we had seen To reproduce this problem without factory_bot, I removed the factory_bot_rails gem from the Gemfile of the provided repo, then ran: story_type = StoryType.new
account = Account.new
account.profile_photo = Rack::Test::UploadedFile.new(Rails.root.join("spec", "factories", "test-image.jpeg"), "image/jpeg")
account.save!
story_type.account = account
#=> ActiveRecord::AssociationTypeMismatch Since this is not coming from factory_bot, I am going to close it. I will probably keep poking around in Rails and spring to see if I can figure out what exactly is going on. For some reason the |
I dug a bit further into this and it appears to be an interaction between spring, the EventedFileUpdateChecker, and the Active Job inline queue adapter. If you remove any one of these things the problem goes away:
I will see if I can get this to the point where I can open an issue on Rails. |
Steps to reproduce
Following up with @hwhelchel on #1029
Isolated the issue here:
When calling
FactoryBot.create(:story)
, theactiverecord-6.0.0/lib/active_record/associations/association.rb#raise_on_type_mismatch!
method is called. Inside,record.is_a?(reflection.klass)
is called which compares the class ofrecord
toreflection.klass
. When you put apry
statement there, you'll see that both classes areAccount
, that said, they have differentobject_id
s causing theis_a?
equality check to fail and then causing theAssociationTypeMismatch
error to be raised. After the error is raised, if you callFactoryBot.reload
, and then runFactoryBot.create(:story)
again it works.Reproduction Script
Expected behavior
Actual behavior
System configuration
factory_bot version: 5.0.2
rails version: 6.0
ruby version: 2.6.3
The text was updated successfully, but these errors were encountered: