Reputation: 147
I have an Ecto Schema with payments containing an amount field. That field's type is the Money.Ecto.Composite.Type from the ex_money package(v. 5.12.3). The query is:
query =
from(
payment in Payment,
where: payment.amount.currency_code() == ^currency
)
This raises:
** (Ecto.Query.CompileError) cannot fetch field `currency_code` from `payment.amount`. Can only fetch fields from:
* sources, such as `p` in `from p in Post`
* named bindings, such as `as(:post)` in `from Post, as: :post`
* parent named bindings, such as `parent_as(:post)` in a subquery
Is there a way to access the attribute and not have Ecto search for a field ?
Upvotes: 1
Views: 209
Reputation: 15515
This should work:
currency = "USD" # atom doesn't work
query =
from(
p in Payment,
where: fragment("(?).amount.currency_code = ?", p, ^currency)
)
Note that currency must be a string but of course you can always use to_string
if you have an atom.
The reason this works is because it uses the Postgres syntax for accessing composite types.
Upvotes: 2