Finding nil has_one associations in where query

后端 未结 3 1734
栀梦
栀梦 2020-12-23 13:57

This may be a simple question, but I seem to be pulling my hair out to find an elegant solution here. I have two ActiveRecord model classes, with a has_one and belongs_to as

3条回答
  •  情话喂你
    2020-12-23 14:48

    Found two other railsey ways of doing this:

    Item.includes(:purchase).references(:purchase).where("purchases.id IS NULL")
    
    Item.includes(:purchase).where(purchases: { id: nil })
    

    Technically the first example works without the 'references' clause but Rails 4 spits deprecation warnings without it.

提交回复
热议问题