Home > Mobile >  How to query timestamp with Frozen Record gem
How to query timestamp with Frozen Record gem

Time:09-07

I have a new timestamp field added to my FrozenRecord table. When I try,

TableName.where("expires_at < ?", Time.now)

this is the error message I get. Link to the line in repo

ArgumentError: wrong number of arguments (given 2, expected 0..1) from /Users/preethikumar/.gem/ruby/3.1.2/gems/frozen_record-0.26.2/lib/frozen_record/scope.rb:105:in `where'

How can I query fields with timestamps?

CodePudding user response:

Error Message:

For this gem where only expects a single argument criterias, moreover it appears to expect the symbol :chain or a Hash (or other object that responds to map and yields key, value).

The gem Docs show "Supported query interfaces" as where(region: 'Europe').

Notably it does not say supports ActiveRecord query interfaces.

Additionally it goes on to show "Non supported query interfaces" such as where('region = "Europe" AND language != "English"').

Proposed Solution:

How can I query fields with timestamps?

While this gem does not appear to support less than (greater than) as part of the natural DSL, in your particular instance you should be able to use a beginless exclusive range instead which will use a CoverMatcher e.g.

TableName.where(expires_at: ...Time.now)

CoverMatcher#match? just uses @value.cover?(other) so this should produce the desired result via ruby's Range#cover?. For example:

t = Time.now 
r = ...t 
r.cover?(t) 
#=> false 
r.cover?(t - 1) 
#=> true 
r.cover?(t   1) 
#=> false
  • Related