Named bindings in Ecto (vs positional bindings)
Positional bindings in Ecto are meant to confuse when buildings large queries across several different functions.
query = Thing
|> join(:inner, [t], x in X, on: t.x_id = x.id)
|> join(:inner, [_, x], y in Y, on: x.y_id = y.id)
Using that query in another function might look like this:
query
|> where([_, x, _], x.type == "Articulated")
But what if the positions change? How am I supposed to know which positions are which when I'm adding to this query out of context?
Named bindings help tremendously here (note the :as
option):
query = Thing
|> join(:inner, [t], x in X, as: :x, on: t.x_id = x.id)
|> join(:inner, [_, x], y in Y, as: :y, on: x.y_id = y.id)
Now I can refer to these things without knowing the position. If the position changes it's all good the additive where statement does not have to change:
query
|> where([y: y, x: x], x.type == "Articulated", y.feel == "Good")
Tweet