Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/ecto/query/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ defmodule Ecto.Query.API do
@doc """
Allows a field to be dynamically accessed.

The source name can be a binding (`p` in `from p in Post`) or a named binding
using `as/1` or `parent_as/1`. The named binding maybe a literal atom or an
interpolation.

The field name can be given as either an atom or a string. In a schemaless
query, the two types of names behave the same. However, when referencing
a field from a schema the behaviours are different.
Expand Down Expand Up @@ -633,6 +637,16 @@ defmodule Ecto.Query.API do
where: field(c, ^doors_or_tires) >= 4
end

def at_least_four(query, doors_or_tires) do
from q in query,
where: field(as(:car), ^doors_or_tires) >= 4
end

def at_least_four(query, binding, doors_or_tires) do
from q in query,
where: field(as(^binding), ^doors_or_tires) >= 4
end

In the example above, `at_least_four(:doors)` and `at_least_four("num_doors")`
would be valid ways to return the set of cars having at least 4 doors.

Expand Down
Loading