Skip to content

Conversation

@koki-miyazaki
Copy link

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.

@palexvs
Copy link

palexvs commented May 22, 2025

I experience the same issue

@palexvs
Copy link

palexvs commented May 22, 2025

@koki-miyazaki wdyt about ^^ ?

@koki-miyazaki
Copy link
Author

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

@moiristo
Copy link
Owner

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

@palexvs
Copy link

palexvs commented May 29, 2025

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.

@koki-miyazaki
Copy link
Author

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
Copy link
Author

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.

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

Successfully merging this pull request may close these issues.

3 participants