salsify / goldiloader

Just the right amount of Rails eager loading

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failure during replace of HABTM associations

tjchambers opened this issue · comments

I have a straightforward HABTM setup:

class Subclass < ActiveRecord::Base
end

class ParticipantSubclass < Subclass
end

class Participant < ActiveRecord::Base
     has_and_belongs_to_many :participant_subclasses,
                          association_foreign_key: :subclass_id
end

Without goldiloader I get no errors from my unit tests.

However when I query with goldiloader in place I get strange results. I have a participant that is associated with 3 participant_subclasses.

when I do the following:

puts @participant.participant_subclasses.size # <= 3
puts @participant.participant_subclasses.to_a.size # <= 1

Visually if I p @participant.participant_subclasses I see one association:

#<ActiveRecord::Associations::CollectionProxy [#<ParticipantSubclass id: 912, classification_id: 257, title: "Development", description: nil, updated_at: "2016-03-29 18:37:47", source: "tChambers@Schellingpoint.com", seq: 1, showhover: true, incpoints: false, excpoints: false, permalink: "912-23ac5e2f", assessment_id: 944, oeuse: false, created_at: "2014-12-31 17:19:23", type: "ParticipantSubclass", active: true, minrespondents: 2>]>

> p.participant_subclasses.map(&:id) #  => [912]
> p.participant_subclasses.ids # => [922, 912, 925]


This is very confusing. The way I found this is in my controller I replace the subclass list using the input:

@participant.participant_subclasses = subclass_list

When it tries to replace the list with the same list from input, it fails because it only recognizes that 1 of the 3 is already in place (912) and tries to add the other 2 and fails immediately adding 922 with a duplicate_key.

BTW - is there a simple mechanism for turning goldiloader universally off? A config parameter I could use in an initiallizer?

BTW I tried to workaround this issue with a .clear on the associations, but that does not help because it is only clearing the 1 and not all 3.

I also tried sticking auto_include: false in and that did not workaround either. SO I feel like I am stuck again turning goldiloader off. This could certainly be a Rails bug, but unless I can turn off this occurrence I have to abandon the gem, which is a pity because it seems to really speed up my app.

I'll take a look later today (a reproducible test case will definitely speed this process). With regards to disabling Goldiloader you can do the following in an initializer to change the default value for auto_include:

ActiveRecord::Associations::Association.default_auto_include = false

You can also change the default for particular types of associations:

ActiveRecord::Associations::HasManyThroughAssociation.default_auto_include = false

I am a noob at the use of goldiloader, but my experience so far (without trying the above :) yet) is that changing adding auto_include: false does not work around my issue. SO I need to try your default value change. Thanks for that.

@tjchambers - I couldn't reproduce this problem. Can you modify this gist to reproduce the problem?

@tjchambers - any luck coming up with a reproducible test case?

@jturkel - it is still on my list. Was targeted for attention last Sunday, but I ended up on a mini-vacation instead. Retargeted for this Sunday. Please keep this open.

@tjchambers - I'm going to close this due to lack of activity. Please reopen it if you can come up with a reproducible test case.