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

Missing relations in new (cloned) model #70

Closed
emiellohr opened this issue Oct 5, 2016 · 4 comments
Closed

Missing relations in new (cloned) model #70

emiellohr opened this issue Oct 5, 2016 · 4 comments

Comments

@emiellohr
Copy link

Im cloning an event object with nested models like show here:

    copied_object = self.deep_clone include: [
      :alternative_tickettexts,
      :conversion_script,
      :event_email_description,
      :memberships,
      :performances,
      :sponsor_images,
      passe_partouts: [:performances, {categories: :products}],
      categories: :products,
      ], use_dictionary: true, validate: false

The result is a cloned object, but misses the categories: :products on the copy. The use of use_dictionary doesn't seem to make a difference. I tried various alternatives, but never ended up with the required result. Can you give me some advice here?

@moiristo
Copy link
Owner

moiristo commented Oct 7, 2016

What kind of association are we talking about? Did it clone the categories? Are the products still missing after saving the copy?

@joshleitzel
Copy link

I've come across this issue as well, and it seems to be due to the use of a multi-item hash inside the include array. The deep_clone method appears to accept these formats:

record.deep_clone(include: { relation1: :attr1, relation2: :attr2 })
record.deep_clone(include: [:relation3, { relation1: :attr1 }, { relation2: :attr2 }])

but does not support using a single hash inside the array if it has multiple keys:

record.deep_clone(include: [:relation3, { relation1: :attr1, relation2: :attr2 }])
record.deep_clone(include: [:relation3, relation1: :attr1, relation2: :attr2])

I agree this is a tad confusing/unexpected since the last format would be the more idiomatic way to write this in Rails these days. I toyed around for a bit with the code but couldn't find a way to make this work without breaking some of the other use cases, but here's a test to show the problem:

def test_multiple_and_deep_include_association_with_array
  deep_clone = @jack.deep_clone(include: [:parrot, treasures: :gold_pieces, mateys: {}])
  assert deep_clone.new_record?
  assert deep_clone.save
  assert_equal 1, deep_clone.treasures.size
  assert_equal 1, deep_clone.gold_pieces.size
  assert_equal 1, deep_clone.mateys.size
end

For now, the second format I showed above (with explicitly separate hashes) can be used if you want to mix array and hash relations.

PS: Thank you for this wonderful gem. It's helped me greatly optimize a tedious workflow in one of my projects. :)

@moiristo
Copy link
Owner

Thanks! I'll check it out soon, should be fixable I think..

@moiristo
Copy link
Owner

moiristo commented Dec 7, 2016

I fixed the test now in 517537d by 'normalizing' the includes list (converting multikey hashes to singular hashes). Should be ok I think?

@moiristo moiristo closed this as completed Jun 1, 2017
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

3 participants