Reputation: 3383
I'm trying to do something like this in my controller:
@inventory_items = @store.inventory_items.where(:updated_at < Time.now - 1.minute)
I keep getting a comparison of Symbol with Time failed
error.
I tried to call to_datetime and to_date on :updated_at
, but perhaps those only work on strings or integers?
How can I get :updated_at
into a proper date format to compare with Time.now - 1.minute
?
Thanks in advance!
Upvotes: 0
Views: 573
Reputation: 11220
Well, there are some ways you can do it.
The reason it doesn't work is because the symbol is only a pointer to the column and not the column itself.
So, either you do
@inventory_items = @store.inventory_items.where(["updated_at < ?", Time.now - 1.minute])
or as an alternative
@inventory_items = @store.inventory_items.where(["updated_at < :one_minute_ago", {one_minute_ago: Time.now - 1.minute]})
Or, you could do
@inventory_items = @store.inventory_items.where.not(:updated_at => Time.now - 1.minute..Time.now)
Upvotes: 3
Reputation: 38645
I do not think with the hash style you can use less than or greater than checks. Try the following:
@inventory_items = @store.inventory_items.where('inventory_items.updated_at < ?', Time.now - 1.minute)
As far as "proper date format" is concerned, you need not worry about them here. All database dates are by default converted to UTC.
Upvotes: 2