bharani91

bharani91

Dynamically composing Ecto queries

I’ve been struggling to compose Ecto queries. I have 2 models - a Person model and a traits model like so -

Person -> has_many -> Traits
Trait -> belongs_to -> Person

The schema for Traits model is like this -

schema "person_attributes" do
    field :name, :string
    field :value, :string
    belongs_to :person, App.Person

    timestamps()
end

What I am trying to achieve is dynamically filter people based on certain traits. Eg: find only those people who have a trait called ‘plan’ with a value of ‘Business’.

I should also be able to add more trait-based filters based dynamically, eg: people who have trait with name == ‘plan’ and value == ‘business’ AND another trait with name == ‘medium’ and value == ‘mobile’.

(From the front-end ui, the user can choose to add more trait-based filters via a dropdown and this query should reflect that trait’s name & value.)

I think this is the query that I need. My only issue is that I am not able to generate it dynamically.

from s in Person,
    where: s.account_id == ^1 and s.state == ^"active",
    join: c in Trait, where: c.person_id == p.id,
    where: (c.name == ^"plan" and c.value == "pro") or (c.name == ^"creation_date", fragment("CAST(coalesce(?, '0') AS integer)", c.value) >= 1470216128)

Any help would be highly appreciated!
Thanks!

First Post!

nerdyworm

nerdyworm

def search(params) do
  query = from s in Person, where: s.account_id == 1

  query = if params.plan != "" do
      where(query, [p], p.name == ^params.plan)
    else
      query
    end
   # other logic for building the query
  query
end

And of course you can use the query variable to compose another like so

query = search(%{q: "search for q"})

query
|> where([p], p.person_id == ^id)
|> Repo.all

This was also a very helpful blog post covering the topic: https://blog.drewolson.org/composable-queries-ecto/

Let me know if this helps.

Cheer,
Benjamin

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joaquinalcerro
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
hariharasudhan94
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement