File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments