sudochop

sudochop

I’ve got an existing database with an interesting permissions model. The permissions schema are based on a php library called Spatie Permissions. Long story short, I need to join my User’s roles through a model_has_roles table. model_has_roles is generic and allows any model in the system to have roles via a model_type field. For sake of brevity, what I’m trying to do is create an association for this query:

from r in Role,
  inner_join: mhr in "model_has_roles",
  on: mhr.role_id == r.id,
  where: mhr.model_id == ^user.id,
  where: mhr.model_type == "App\\User"

Unfortunately the docs say :where - A filter for the association. See "Filtering associations" below. It does not apply to :through associations.

Am I out of luck? Ultimately I just want track the roles / preload them right on the User model.

Showing Posts 1 to 5

al2o3cr

al2o3cr

I’d expect this to work (in the User schema):

has_many :model_has_roles, foreign_key: :model_id, where: [model_type: "App\\User"]
has_many :roles, through: [:model_has_role, :role]

along with a standard belongs_to :role on ModelHasRole

sudochop

sudochop OP

This has me so close! I can’t figure out this error though - even though it’s seemingly running the correct query.

iex(3)> Repo.all(from u in User, preload: :roles)

** (ArgumentError) schema ModelHasRole does not have association Role (if you were trying to pass a schema as a query to preload, you have to explicitly convert it to a query by doing `from x in Role` or by calling Ecto.Queryable.to_query/1)
    (elixir 1.11.2) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto 3.5.7) lib/ecto/repo/queryable.ex:235: Ecto.Repo.Queryable.execute/4
    (ecto 3.5.7) lib/ecto/repo/queryable.ex:17: Ecto.Repo.Queryable.all/3

Even though I have the association on the ModelHasRole:

belongs_to :role, Role
sudochop

sudochop OP

Actually it looks like I had a typo. The issue now is:

iex(9)> Repo.all(from u in User, preload: :roles)
** (ArgumentError) cannot preload through association `model_has_roles` on `User`. Ecto expected the ModelHasRole schema to have at least one primary key field

ModelHasRole indeed has @primary_key false.

sudochop

sudochop OP

However this works fine..:

Repo.all(from u in User, preload: [model_has_roles: :role])
sudochop

sudochop OP

So I think I figured this out. I was going about it completely wrong. I think this does what I need:

many_to_many :roles, Role,
  join_through: ModelHasRole,
  join_keys: [model_id: :id, role_id: :id],
  join_where: [model_type: "App\\User"]
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews