Add field in ecto schema that refers to a single instance of a many-to-many relationship

I have a user that has many scopes, and a scope can be had by many users. This relationship has a join table named user_scope However, users also have a current_scope, which mandates some functionality that they can do. This current scope is mapped with a foreign key current_scope_id which points to the user_scope table, not to the scope table. I want to have a field in my users schema that maps the current_scope_id directly to the scope table, similar to how the many_to_many method has a :through option to make many-to-many relationships accessible without having to perform any extra steps.

Is there any easy way to do this that doesn’t involve having to manually load the course?

The has_one/3 macro also has the option to pass a :through key (Ecto.Schema — Ecto v3.8.3).
That would probably work for your problem.

1 Like

Yup it does, thank you :smiley: