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模型,該模型返回沒有寵物的人。有任何想法嗎?我覺得這很明顯,但目前它正在逃避我。查找軌中沒有關聯記錄的記錄3
產生的SQL失敗:SELECT *人,pets.id FROM'persons'左外連接的寵物,其中persons.id = pets.person_id WHERE'persons'。 'deleted' = 0 AND(pets.id爲空) – Shagymoe 2011-03-16 23:32:00
糟糕!我打錯了那個連接。它應該是「...在persons.id = pets.person_id」,而不是「... where persons.id = ...」。 – 2011-03-16 23:55:53
你需要選擇('persons。*,pets.id')中的「,pets.id」嗎?它有可能與persons.id發生衝突,除非你像這樣對列進行別名化:select('persons。*,pets.id as pet_id') – mkirk 2011-04-01 20:16:44