You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
when digging a bit I found that here although models.first.id is set correctly, models.first.association(:drivers).target.first.vehicle.id is nil like its a copy. so since vehicle_id, the foreign key is not set then changed? returns false
Shouldn't this foreign key assignment be done before, so if the only change is the belongs_to association it still detects a change ?
deffind_associated_objects_for_import(associated_objects_by_class,model)associated_objects_by_class[model.class.name] ||= {}returnassociated_objects_by_classunlessmodel.idassociation_reflections=model.class.reflect_on_all_associations(:has_one) +
model.class.reflect_on_all_associations(:has_many)association_reflections.eachdo |association_reflection|
associated_objects_by_class[model.class.name][association_reflection.name] ||= []association=model.association(association_reflection.name)association.loaded!# Wrap target in an array if not alreadyassociation=Array(association.target)# This line is addedassociation.each{ |child| child.public_send("#{association_reflection.foreign_key}=",model.id)}changed_objects=association.select{ |a| a.new_record? || a.changed?}changed_objects.eachdo |child|
# Removed the foreign_key assignment here# For polymorphic associationsassociation_name=ifmodel.class.respond_to?(:polymorphic_name)model.class.polymorphic_nameelsemodel.class.base_classendassociation_reflection.type.trydo |type|
child.public_send("#{type}=",association_name)endendassociated_objects_by_class[model.class.name][association_reflection.name].concatchanged_objectsendassociated_objects_by_classend
Current workaround is also to add driver.send(:mutations_from_database).force_change('vehicle_id') right after assigning the new vehicle to the existing driver. The fact that mutations_from_database is a private method is kind of an indicator that this is not really what it's made for
The text was updated successfully, but these errors were encountered:
Hi 👋
I'm trying to insert multiple new records with a
has_many
association but it seems like it's not working correctly in this casewhen digging a bit I found that here although
models.first.id
is set correctly,models.first.association(:drivers).target.first.vehicle.id
isnil
like its a copy. so sincevehicle_id
, the foreign key is not set thenchanged?
returnsfalse
Shouldn't this foreign key assignment be done before, so if the only change is the belongs_to association it still detects a change ?
changing
activerecord-import/lib/activerecord-import/import.rb
Lines 977 to 1009 in e3331d5
Current workaround is also to add
driver.send(:mutations_from_database).force_change('vehicle_id')
right after assigning the newvehicle
to the existingdriver
. The fact thatmutations_from_database
is a private method is kind of an indicator that this is not really what it's made forThe text was updated successfully, but these errors were encountered: