I have an Order
model with those attributes:
- created_at DateTime
- delivery_at DateTime
- status String
What I am trying to do, is to sort two collections by created_at
or delivery_at
depending on its status
.
On the model, I created a method which checks the status order and return created_at
or delivery_at
datetime.
def status_date
if status == 'on_delivery'
delivered_at.to_datetime
else
created_at.to_datetime
end
end
And then in the controller:
created = Order.where(status: 'open')
on_delivery = Order.where(stauts: 'on_delivery')
orders = created + on_delivery
orders.sort_by(&:status_date)
Which is not working.
I am trying to achieve a chronological list of orders, but DateTime should be related to its status.