deep_cloneable icon indicating copy to clipboard operation
deep_cloneable copied to clipboard

Add order on deep cloning has_many associations

Open koki-miyazaki opened this issue 1 year ago • 5 comments

Currently when cloning a record with has_many associations, the order of the value of primary key of the associated records is not preserved. PostgreSQL and sqlite3 don't guarantee the order of the primary keys in the result set unless an ORDER BY clause is used.

koki-miyazaki avatar Dec 26 '24 01:12 koki-miyazaki

I experience the same issue

palexvs avatar May 22 '25 22:05 palexvs

@koki-miyazaki wdyt about ^^ ?

palexvs avatar May 22 '25 22:05 palexvs

@moiristo Hi, could you have a look at the PR? Thanks!

koki-miyazaki avatar May 27 '25 01:05 koki-miyazaki

Hi, thanks for the PR. Could you elaborate on why the order is important on duplication? And if so, would defining a scope on the association work as well?

has_many :things, -> { order(:id) }, dependent: :destroy

moiristo avatar May 27 '25 07:05 moiristo

Could you elaborate on why the order is important on duplication?

I have example project with sub-entities that I copy into each new user account. It's important to create the example identical to original with the same tasks and other items order.

And if so, would defining a scope on the association work as well?

Good question. I'm considering it, but I'm hesitant because it's an old and complex project. Updating the main associations just for one small, specific case doesn't seem justified at this point.

palexvs avatar May 29 '25 08:05 palexvs

would defining a scope on the association work as well?

It should work 👍 But the order is for cloning record, so implementing it in deep_cloneable is not so bad idea? How do you think? @moiristo

koki-miyazaki avatar Jul 30 '25 01:07 koki-miyazaki

why the order is important on duplication

In my project there is a relation a campaign has_many prizes, the prizes are shown ordered by id on UI. Cloning campaign should not change the order in UI for UX.

koki-miyazaki avatar Jul 30 '25 02:07 koki-miyazaki