File tree Expand file tree Collapse file tree 1 file changed +14
-0
lines changed Expand file tree Collapse file tree 1 file changed +14
-0
lines changed Original file line number Diff line number Diff line change @@ -603,6 +603,10 @@ defmodule Ecto.Query.API do
603
603
@ doc """
604
604
Allows a field to be dynamically accessed.
605
605
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
+
606
610
The field name can be given as either an atom or a string. In a schemaless
607
611
query, the two types of names behave the same. However, when referencing
608
612
a field from a schema the behaviours are different.
@@ -633,6 +637,16 @@ defmodule Ecto.Query.API do
633
637
where: field(c, ^doors_or_tires) >= 4
634
638
end
635
639
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
+
636
650
In the example above, `at_least_four(:doors)` and `at_least_four("num_doors")`
637
651
would be valid ways to return the set of cars having at least 4 doors.
638
652
You can’t perform that action at this time.
0 commit comments