ketupia
User group membership with roles?
I’m trying to create a many_to_many relationship between User (Ash Authentication) and Group (or a team, etc) with a role. I’m looking to use this to track which users belong to which groups and to enforce policies.
I’ve created a join resource GroupMember with a role attribute and belongs_to relationships but for the life of me I can’t figure out manage_relationships with setting the :role on the join.
Anyone have an example I could learn from?
defmodule GroupMember do
attributes do
uuid_primary_key :id
attribute :role, :atom, constraints [one_of: [:admin, :moderator, :member]]
end
relationships do
belongs_to :group, Group
belongs_to :user, User
end
end
defmodule User do
...
relationships do
many_to_many :groups, Group do
through GroupMember
end
end
actions do
update :join_group do
require_atomic? false
argument :group_id, :uuid do
allow_nil? false
end
argument :role, Hestia.Accounts.Role do
allow_nil? false
default Hestia.Accounts.Role.default()
end
change manage_relationship(???)
end
end
...
end
Marked As Solved
zachdaniel
Creator of Ash
Since you aren’t taking a :map argument, you’ll need to call Ash.Changeset.manage_relationship in a change yourself. Additionally, this can be made much simpler by managing the join relationship directly. It can be done when managing the many_to_many its just a bit more annoying.
- Give the join relationship a better name. A
has_manyjoin relationship is always created, called<relationship_name>_join_assocformany_to_many. It exists to make the underlying logic work, but is given an ugly name to encourage you to name it yourself if you want to use it for something. You don’t need to here, but this also allows you to define the join relationshiphas_manyand customize its behavior if you need to.
many_to_many :groups, Group do
through GroupMember
join_relationship :group_members
end
- now, manage the join relationship directly
change fn changeset, context ->
Ash.Changeset.manage_relationship(
changeset,
:group_members,
[%{
role: changeset.arguments[:role],
group_id:changeset.arguments[:group_id]
}],
type: :create
)
end
1
Popular in Questions
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
Hi,
is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Hi,
I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I would like to know what is the best IDE for elixir development?
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








