我试图返回关联是否存在的记录:
我试过这些望远镜:

class Booking < ActiveRecord::Base
  has_one :availability

  scope :with_availability, -> {where{availability.not_eq nil}}
  scope :without_availability, -> {where{availability.eq nil}}
end

最佳答案

试试这个:

class Booking < ActiveRecord::Base
  has_one :availability

  scope :with_availability, -> { joins{availability} }
  scope :without_availability, -> { joins{availability.outer}.where{availability.id.eq nil} }
end

关于ruby-on-rails - 范围和关联不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32770545/

10-11 02:27