0
實現這個我直接使用此查詢,並希望使用ActiveRecord,如何在ActiveRecord的
SELECT count(*)
FROM p
LEFT JOIN
(SELECT pid
FROM s LEFT JOIN i ON s.key = i.sid
WHERE i.default = 'y') AS table_x
ON p.pid = table_x.pid WHERE isnull(table_x.pid) AND p.show = 'y'
但我不太清楚如何實現以上。我目前的定義如下。
class P < ActiveRecord::Base
has_many :s, :foreign_key => 'pid'
has_many :i, :through => :s
end
class S < ActiveRecord::Base
belongs_to :p, :foreign_key => 'pid'
has_many :i, :foreign_key => 'sid'
end
class I < ActiveRecord::Base
belongs_to :s, :foreign_key => 'sid'
belongs_to :p, :through => :s
end
我很想知道的部分是關於如何創建/將子查詢作爲表格/模型?
pid定義表s或我嗎? –