0

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?

3 Answers 3

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

In Rails 5 you can actually already use 'or' method for ActiveRecord relations:

Order.where(status: :picked_up, pickup_date: Date.today).or(Order.where(status: :delivered, delivery_date: Date.today))
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.