settheline
settheline

Reputation: 3383

RoR compare timestamp symbol to Time.now date in controller

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

Answers (2)

Jimmy Stenke
Jimmy Stenke

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

vee
vee

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

Related Questions