vinhnglx / vinhnglx.github.io

0 stars 0 forks source link

TIL_26_Jul_2017 - Query in Ecto #26

Open vinhnglx opened 7 years ago

vinhnglx commented 7 years ago
alias Mandeeoo.Repo
alias Mandeeoo.Type

query = from t in Types, 
  join: b in Blog, on b.type_id == t.id
  select: t.name

Repo.all(query)
alias Mandeeoo.Repo
alias Mandeeoo.Type

query = Type
query = from t in Query, join: b in Blog, on b.type_id == t.id
query = from t in Query, select: t.name

Repo.all(query)

We can write the query in small steps. This strategy works because Ecto defines something called the Queryable protocol.

Queryable Protocol: Converts a data structure into an Ecto.Query.