Hi, is there a way to have named association, please? Akin to Rails/ActiveRecord as:
option. Something like this:
many_to_many :users, as: :members
I just can’t find it anywhere. Which leads me to believe it’s probably done some other way? Thank you.
If I’m not mistaken this is already possible. You pick a name and then specify how this is sourced:
many_to_many :tags, MyApp.Tag, join_through: "posts_tags"
4 Likes
This just specifies the join table name, doesn’t it? I have two models: User and Class. I have many to many association - each user can enroll to many classes, each class has many members. And I want to call them “members” rather than “users”, to make the naming a bit less misleading.
You can freely choose how you name the many_to_many
field on a schema. There’s some defaults based on field name, but all of them can be also manually set to match how things are named in the db.
1 Like
many_to_many :members, YourApp.User, join_through: "classes_users"
should do it for you.
You can then do: YourApp.Repo.preload(class, :members)
and you will get the users
as values in a members
key.
1 Like