Cast join_table with another function?

With cast_assoc I can use the with parameter, but how can I use the with parameter with join_table in a many_to_many relation?

In general, if you need more control over the join table in a many-to-many situation it’s better to make the creation / insertion of that record explicit (instead of hidden inside of many_to_many).

In this specific case, I’m not following what you want. For concreteness, imagine we’re working with the UserOrganization example from the many_to_many docs.

My interpretation of your question is that you want to somehow produce a call to UserOrganization.changeset when doing cast_assoc(user_changeset, :organizations), but this raises another question: what parameters are you hoping to get in the call to UserOrganization.changeset? The input for :organizations has data for Organization, but not the join record.

Some other options to consider:

  • use the join_defaults option to many_to_many to set columns on the join record
  • make the many_to_many a pair of has_many associations (user_organizations and organizations) and then explicitly cast_assoc the join table association when extra data is needed on the join records
2 Likes