3 ms·
Understanding Associations in Elixir's Ecto
- dqv 6y agoWith put_assoc, at least for me, I expected it to work the same way when creating a record vs editing a record, but that isn't the case. While put_assoc is additive when you are first creating the record, the same is not true when modifying a record. You have to provide all the original data in addition to the new record when modifying it. Comments for that reason aren't a good use case for put_assoc, because generally you add a comment one at a time. It makes more sense to just create a new comment with the associated post ID. Also the string keys in structs weirded me out - I don't think that works in Elixir.
- eyelidlessness 6y agoBased on your description, it sounds like it does behave the same way on create and update. In every case, you have to supply the whole dataset for the relations. Not being familiar with Ecto or Elixir (and only moderately familiar with Erlang), I would expect anything named `put` to be a full replacement. That's what I associate with the same verb in HTTP semantics, as well as in general FP semantics. It seems like a welcome place for an abstraction like `cons`, but I think even that may get into impedance mismatch territory...
- deathtrader666 6y agoI've found it a bit challenging to wrap my head around many-to-many associations in Ecto..
- AlchemistCamp 6y agoCoincidentally, I've been working on a screencast series on YouTube the past couple of weeks and the very next video in it will cover setting up many-to-many associations and has-many-through associations. I just shared it here: https://news.ycombinator.com/item?id=25066424 https://news.ycombinator.com/item?id=25066424
- elcritch 6y agoExcellent! The many-to-many relationships in Ecto still confuse me. Though, in my defense, most of my work is using embedded KV not SQL so Ecto is only for the UI form stuff.
- asenna 6y agoCame across Alchemist.camp a few weeks ago when I was starting out learning Phoenix - they're awesome, nice work! Would recommend people checkout the list: https://alchemist.camp/episodes https://alchemist.camp/episodes
- dugmartin 6y agoFor some meta info about Alchemist.camp the author is part of a two-person podcast where he talks about it here: https://reactor.am/podcasts https://reactor.am/podcasts I've been listening to the other person on the podcast (Justin Vincent) other podcast, Techzing, for many years.
- malloryerik 6y agoYou might also find help in the free and very good "Little Ecto Cookbook". See "Constraints and Upserts" as well as "Polymorphic associations with many to many". https://dashbit.co/ebooks/the-little-ecto-cookbook https://dashbit.co/ebooks/the-little-ecto-cookbook