Add change_order option to has_many#4722
Add change_order option to has_many#4722greg-rychlewski wants to merge 8 commits intoelixir-ecto:masterfrom
change_order option to has_many#4722Conversation
|
As a side note I don't think If it's alright I can fix that in a follow up. |
|
Actually nm I'm just dumb and didn't set up the test data correctly. Added the test |
|
@greg-rychlewski could this be a changeset function instead? Something when casting associations or a pass we do on the changeset data after cast/put_assoc? |
|
@josevalim What about something like this cast_assoc(changeset, :assocs, change_order: :unique_safe)We would still do the sorting where it is in this PR but now the user has the flexibility to change the order instead of it being hard coded onto the schema. The reason I'm not sure about |
|
I was thinking something like: |
|
Ah ok. Just to make sure I understand correctly you are saying reorder_assoc always affects the database operation order right? So cast |
|
So there are two possible ways for the changesets for an assoc to be created:
So my idea is that you can call reorder_assoc before you write to the database. We can provide a default function (no argument, but you can also give a custom one). Does that make sense? |
|
Oh yes I got your idea now. Thanks! |
|
@josevalim I'm having a bit of doubt and was curious what you thought. I started to implement it and then I thought that Do you think it would make sense to expose specific sort functions for users to apply themselves? I can't think of a good name right now but the idea would be something like |
Closes #4713
I had a few different thoughts how to do this...some of the alternatives I thought about:
:insert,:update,:deleteand letting the user choose which order they want. I thought it might get messy though validating that the list makes sense (i.e. if there are duplicates of certain atoms or things are missing) and also if potentially people will want to handle:replacespecially:replaceinto repo actions that might be tricky to handle:unique_safeI thought about making it something like:remove_firstor:delete_firstor:delete_upsert_insert. The first two I discarded because I thought they were a bit confusing/not completely accurate. The last one I thought is a bit long and opens it up to having all the permutations as option names. I decided to try to find some kind of short user-friendly name that encompasses the specific purpose.