How do I write this in Ecto Query?

I have a designs table that has these field

design_id
design_type
user_id

and design table belongs to user table. So user has_many designs and designs belongs to user.

first I need to query on design_id and design_type and load all the designs.

Which I did something like this

from([a] in Design, where: a.design_type == ^type and a.design_id == ^id)

Where I will get results like this

[
%Design{
    design_id: 1,
    design_type: "document",
    user: #Ecto.Association.NotLoaded<association :user is not loaded>,
    user_id: 2,
  },
%Design{
    design_id: 1,
    design_type: "document",
    user: #Ecto.Association.NotLoaded<association :user is not loaded>,
    user_id: 3,
]

So, I want to get a list of users for the same design_id and design_type.

Also if I’m querying ` from([a] in Design, where: a.design_type == ^type and a.design_id == ^id) from elixir shell its giving me a list of designs.

But when I tried to use it in function

  def design_filters(%{"design_id" => id, "design_type" => type}) do
    from([t] in Design,
      where: t.design_id == ^id and t.design_type == ^type
    )
  end

I’m getting this error

** (FunctionClauseError) no function clause matching

Change [a] to just a?

Still the same error.

Also, my focus is on this right now. Do you have any suggestions here?

from the top of my head you would need something like:

Ecto:

from d in Design
 join: u in User, on: d.user_id == u.id,
 where: d.design_type_id = ^type and d.design_id = ^id,
 select: u

actually even more simple if you don’t need the user table only the id:

from d in Design, where: d.design_type_id = ^type and d.design_id = ^id, select: d.user_id
2 Likes

Thanks :slight_smile: