svelandiag
svelandiag

Reputation: 4320

get value within rails scope

I want to create a scope, I want to get all Orders in which has passed more than 24hrs since the last time they were updated.

I want to get all those Orders for then loop in them:

Order.more_than_24_updated.find_each do |order|
  #do some stuff with each order
end

I do not know how to add 24hrs to the updated_at attribute of orders in the scope:

scope :more_than_24_updated, -> { where(status: "pago_pendiente").where('#{Time.now} >= ?', updated_at + 24.hours) }

Well I know the above does not work, but well... how to do this?

Upvotes: 0

Views: 110

Answers (2)

potashin
potashin

Reputation: 44581

You can try something like this:

scope :more_than_24_updated, -> {where('status = ? and updated_at <= ?', :pago_pendiente, 24.hours.ago)}

Upvotes: 1

Hieu Pham
Hieu Pham

Reputation: 6707

You can try this one

scope :more_than_24_updated, -> { where(status: "pago_pendiente").where("NOW() - '24 hours'::INTERVAL >= updated_at") }

Upvotes: 1

Related Questions