Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Import with synchronize docs issue #782

Open
gvaibhav1734 opened this issue Sep 23, 2022 · 0 comments
Open

Import with synchronize docs issue #782

gvaibhav1734 opened this issue Sep 23, 2022 · 0 comments

Comments

@gvaibhav1734
Copy link

Issue

Documentation needs to call out the actual working of :synchronize.

Example

Let's say we have a ActiveRecord model Item and we are using something like:

class Item < ApplicationRecord
  def self.is_active?
    return @active if @active
    @active = self.active
  end
end

# In controller
def some_function
  @items = Item.all
  # some changes to @items which changes :active field
  Item.import(@items, synchronize: @items, synchronize_keys: [:shop_id, :cart_id], on_duplicate_key_update: :all)
end

The expectation after import is that @items got synchronized with import updates in place. However, the @active instance variable is not reset which should be explicitly called out in the documentation.

Code explanation

instance.instance_variable_set :@attributes, matched_instance.instance_variable_get(:@attributes)

From this file (and line)

  1. It is evident that the instance variables @attributes are being set in memory with fresh values from import call.
  2. Its also clear that only the instance variables belonging to ActiveRecord (like @new_record @destroyed) are being changed.

Docs should explicitly call this out because if @items had its own custom instance variables they wont be replaced or reset to nil.

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

No branches or pull requests

1 participant