class Person < ActiveRecord::Base
has_many :pets
scope :with_dog, join(:pets).where("pets.type = 'Dog'")
scope :without_pets ???????????????????????????????????
end
class Pet < ActiveRecord::Base
belongs_to :people
end
我想向 Person 模型添加一个范围,以返回没有宠物的人。有任何想法吗?我觉得这很明显,但此刻它正在逃避我。
最佳答案
scope :without_pets, lambda { includes(:pets).where('pets.id' => nil) }
关于ruby-on-rails - 在 rails 3 中查找没有关联记录的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5332093/