0

What do I want

What I need to get all orders without acceptance for a given user, but I'm having issues with the first part only.

What do I have

Here are my models

class Order < ApplicationRecord
  has_one :acceptance
end

class Acceptance < ApplicationRecord
  belongs_to :order
end

What have I tried

I have already checked this answers

But they applied for a has_many - belongs to scenario (which is, presumably, why they fail). My scenario is simpler: has_one - belongs_to.

So far (after trying by myself, and reading other SO questions) the following have failed.

  • Order.includes(:acceptance).where(acceptance: { id: nil})
  • Order.left_outer_joins(:acceptance).where(acceptance: { id: nil })
  • Order.joins(:acceptance).merge(Acceptance.where(order_id: nil, id: nil))

Moreover, I can't do Order.joins(:acceptance) because that immediately fetches orders with an acceptance associated, I want the opposite of that.

Didn't seem that hard a priori, but it's giving a mild pain.

Sebastialonso
  • 1,437
  • 18
  • 34

1 Answers1

1

Order.includes(:acceptance).where(acceptances: { id: nil}) should work. You have missed s at the end. When you construct the query using where statement with nested hash parameters, first key is always table name

Peter Balaban
  • 653
  • 3
  • 7