Add attribute using `many_to_many` relationship DSL

I don’t think this is possible, but thought I’d pose the question (I did see this, which is similar but specifically asking if the relationships DSL somehow makes it easier to implement).

Is it possible to create a many to many relationship with an attribute, e.g., along these lines:

relationships do
  many_to_many :members App.Accounts.User do
    through App.Team.Members
    source_attribute_on_join_resource :team_id
    destination_attribute_on_join_resource :user_id

>   attribute :role, :atom do # this bit... can't be done?
>     allow_nil? false
>     constraints [one_of: [:team_member, :team_lead]]
>   end
  end  
end

From what I can see in the relationships DSL adding attributes like this isn’t supported. I can see how that could get difficult.

On the other hand – having very simple support would be awesome, e.g., in this case I just want a :role identifier that must be set. No magic and no tricky other stuff. It could potentially save a good bit of overhead (defining a new property, etc., etc.)

Is this worth writing up as a feature request?

You’d add that attribute as normal onto your App.Team.Members resource.

Ah, right, gotcha.

And then… if I want a policy “only team leads can update the team” I could do something like this?

policy action_type(:update) do
  authorize_if relates_to_actor_via([:members, :user_id])
  authorize_if eval(:members.role == :team_lead)
end

Every many_to_many has an implicit join relationship, unless you configure its name manually, i.e join_relationship :memberships. It defaults to <relationship>_join_relationship

authorize_if expr(exists(members_join_relationship, role == :team_lead and user_id == ^actor(:id)))

Thanks @zachdaniel that definitely helps clarify. :+1: