I am trying to filter records by a calculated interval.
My resource has these two attributes:
attribute :retry_at, :utc_datetime, allow_nil?: true
attribute :tried_at, :utc_datetime, allow_nil?: true
I tried the following calculation:
calculations do
calculate :interval, :time, expr(retry_at - tried_at)
end
Then the following query
ten_minutes = Time.new!(0, 10, 0)
Red.Practice.Card
|> Ash.Query.filter(interval > ^ten_minutes)
|> Red.Practice.read!()
This felt like amazing, because it composed a SQL query that actually worked. The calculation even returned a nice nil
when one of the values are missing.
Except that my time
type for the calculation is wrong. It can not handle more than a day, because a Time
in Elixir represents a time of day.
I tried this:
calculate :interval, :naive_datetime, expr(fragment("retry_at - tried_at"))
But then I get the following error:
(ArgumentError) cannot load `%Postgrex.Interval{months: 0, days: 0, secs: 600, microsecs: 0}` as type #Ash.Type.NaiveDatetime.EctoType<[]>
How can I create a good interval
calculation on which I can filter?