Named Bindings

Work with named bindings

Keeping track of the query bindings isn’t too difficult with a small query like those we’ve used previously. The query composition spanned a small section of code and we only had two bindings. With a more complex query spread over a wider area, though, it could be tough to remember which binding needs to go where.

To help alleviate this problem, Ecto allows us to assign specific names to bindings that it saves throughout the query’s life. Let’s look at how this works.

Create a named binding

We can use the as: keyword to create a named binding. We can add this to the from call or any of the join options. Here’s how we could rewrite our last query with named bindings.

Get hands-on with 1400+ tech skills courses.