Where to deal with Associations in Phoenix?

So as newbie to Phoenix I have been playing around and reading a lot and there seems to be three places with dealing with Associations in Phoenix, but I am not sure which is best practice:

  1. In the Changeset in the schema file. I think this works best for certain types of association, but others such as M2M put_assoc and cast_assoc don’t seem to capture every case easily. It doesn’t seem correct to me to put it in the Changeset, but I am not an experienced Phoenix developer.
  2. In the Controller, such as web controller. I have seen many examples of others doing this, it seems like the easiest place but seems like it’s not as reusable.
  3. In the Context (such as create_todo) so that on creating or updating etc, could deal with the association.

I guess the answer is depends, but in my particular use-case with several many_to_many associations I am leaning towards putting somethings in the Context files, and having separate context for a join_through Table to make associations. Is this a bad idea?

Thanks in advance

1 Like

I’m lately always putting such logic in context modules. Even if the code gets a bit bigger, the overall readability and comprehensibility of the code is hugely improved.

1 Like