문제

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