moiristo / deep_cloneable

This gem gives every ActiveRecord::Base object the possibility to do a deep clone that includes user specified associations.
MIT License
785 stars 89 forks source link

Second load of records (n+1 issue) #89

Closed APiercey closed 6 years ago

APiercey commented 6 years ago

Hello, first I'm sorry if this is the wrong place to ask (it's a question but I might be highlighting a limition as well, not sure).

Second, thank you for the great work :). On to my question:

When I am cloning an object that includes associations (and possible nested associations), I'd like to include those objects ahead of time, example:

Let's say I have a User and that user has many Lists. I was expecting that:

User.includes(:lists).find(user_id).deep_clone(include: :lists).save

would clone the lists that are already eager loaded from User.includes(:lists).find. It seems that when save is called, each object is retrieved again from the database.

Am I misunderstanding something about how this deep_cloneable works with eager loading? How can I use eager loading and speed up complex clones?

moiristo commented 6 years ago

Hello,

Sorry for the late response, didn't get a GH notification. The code will just use the association, so this should work.. It may be that objects are being loaded in the List object? Maybe specifying inverse_of in your associations may help if you've not already done so.

APiercey commented 6 years ago

Hello @moiristo! Thanks for the response :) unfortunately, this has been put on hold for me so I have no updates. I'll close the issue for now.

Thanks!