简体   繁体   中英

Write activerecord query in rails

I am having "Order" model with pickup_date, delivery_date and status. So i want to select orders -

  • if status if picked_up then pickup_date should be some date value (eg, '12/12/2012')
  • if status if delivered then delivery_date should be some date value (eg, '12/12/2012')

I think we can use WHEN CASE with Activerecord. Can anybody help me in this?

Order.where("(status = 'pickup_up' AND pickup_date = :date) OR (status = 'delivered' AND delivery_date = :date)", date: Date.today)

在Rails 5中,您实际上已经可以对ActiveRecord关系使用“或”方法:

Order.where(status: :picked_up, pickup_date: Date.today).or(Order.where(status: :delivered, delivery_date: Date.today))

In addition to @M. Stavnycha answer, Rails has a great section on ActiveRecord query interface available here http://guides.rubyonrails.org/active_record_querying.html

Order.where(status: :picked_up).update(:pickup_date '12/12/2012')

Order.where(status: :delivered).update(:delivery_date '12/12/2012')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM