Skip to content

Commit ab64e1d

Browse files
Update field/2 docs to say we support as/1 and parent_as/1 (#4663)
1 parent 95b6a29 commit ab64e1d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/ecto/query/api.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,10 @@ defmodule Ecto.Query.API do
603603
@doc """
604604
Allows a field to be dynamically accessed.
605605
606+
The source name can be a binding (`p` in `from p in Post`) or a named binding
607+
using `as/1` or `parent_as/1`. The named binding maybe a literal atom or an
608+
interpolation.
609+
606610
The field name can be given as either an atom or a string. In a schemaless
607611
query, the two types of names behave the same. However, when referencing
608612
a field from a schema the behaviours are different.
@@ -633,6 +637,16 @@ defmodule Ecto.Query.API do
633637
where: field(c, ^doors_or_tires) >= 4
634638
end
635639
640+
def at_least_four(query, doors_or_tires) do
641+
from q in query,
642+
where: field(as(:car), ^doors_or_tires) >= 4
643+
end
644+
645+
def at_least_four(query, binding, doors_or_tires) do
646+
from q in query,
647+
where: field(as(^binding), ^doors_or_tires) >= 4
648+
end
649+
636650
In the example above, `at_least_four(:doors)` and `at_least_four("num_doors")`
637651
would be valid ways to return the set of cars having at least 4 doors.
638652

0 commit comments

Comments
 (0)