Add support for :writable option on belongs_to#4624
Conversation
|
Looks good to me, but I wonder if @greg-rychlewski has any additional thoughts?
How do you think it would be handled? I can still see someone using cast_assoc for something else, even though they never write the field, so perhaps we should not change anything. |
|
My feeling is to leave it out of cast_assoc/put_assoc as well just because it's not as obvious if there are use cases that will screw up. I think as long as its being handled before the persistence happens it's ok. |
|
💚 💙 💜 💛 ❤️ |
|
Reminding myself I need to look to see if this is affected at all #4299 |
|
@josevalim There is a bit of a weird interaction. This test passes: test "insert unwritable assoc" do
assoc = %MyAssoc{x: "xyz"}
changeset =
%RestrictedSchema{}
|> Ecto.Changeset.change
|> Ecto.Changeset.put_assoc(:unwritable, assoc)
schema = TestRepo.insert!(changeset)
assoc = schema.unwritable
assert assoc.id
assert assoc.id == schema.unwritable_id
endSo if you insert the foreign key directly the value is ignored. If you insert the assoc then the foreign key is saved. I'm thinking it's weird if the assoc record is inserted but the foreign key silently isn't updated. And it's also weird if it just saves the foreign key and ignores the option. Maybe the best option is to also not insert the assoc? We might have to add a new |
|
Great catch. I agree we should either make it so it is not written or revert this. |
|
I’ll give it a shot seeing what it would take to make it not written |
|
I'm also happy to contribute if you can point me in the right direction. |
|
In Where it gets complicated is And then if the new value of the associated record is |
|
It's also possible you can combine all those checks I mentioned above in the |
|
I reverted this for now, as we had to do a new release. Please include the old commit once you push the changes above. :) Thank you! |
This reverts commit 576665f.
|
@josevalim The revert doesn't seem to be in the master branch |
|
done! |
PR for my proposal to add
:writableoption tobelongs_to.This PR simply whitelists the
:writableoption in@valid_belongs_to_options, which has already been implemented in #4335 & #4472.cast_assoc&put_assoccurrently do not respect this option, so if it makes sense, I can send a follow-up PR for that.