質問

I wonder if there is Rails magic to avoid searching the database twice in the following situation:

Background: I am searching for orders from a user which are either PENDING or SENT and due today.

Here is the pseudo code:

order_array = Order.where(:filled => pending) || Order.where(:fileld => done, :due => today)

Now the problem is that this searches the database twice. I'd like to do this in one search and would appreciate pointers into how RoR would do it.

役に立ちましたか?

解決

I think this is what you are looking for:

order_array = Order.where("filled = 'PENDING' OR (filled = 'SENT' AND due BETWEEN ? AND ?)", Time.zone.now.beginning_of_day, Time.zone.now.end_of_day)

他のヒント

Try like below chain condition -

Customer.where(:reseller_id => :map_reseller_id).where(:is_deleted_by_reseller => false)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top