A
A
Alexander Petrov2017-03-05 21:43:50
Ruby on Rails
Alexander Petrov, 2017-03-05 21:43:50

How to do the reverse side of joins?

Friends, help! The task is simple along the way, but I can not finish it. and I can't find it in the documentation.
There is, for example, such a thing:
expired_order=Order.joins(:order_expired)
If the application has expired, then a new OrderExperied is added to the database, which has a belongs_to to the Order model.
That is, this code will display all orders that have an attached OrderExperied.
but I need the opposite to display all tasks that do NOT have an attached OrderExperied.
To be something like this:
expired_order=Order.joins.not(:order_expired)
How to implement this correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CapeRatel, 2017-03-05
@Mirkom63

Order.includes(:order_expired).where(order_expireds: { order_id: nil })

dig this way

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question