Rails3のactiverecordの規則を使用してこのクエリを作成しようとしています。
SELECT s.name, s.id FROM tripoli_schools_development.campus_programs cp
JOIN tripoli_schools_development.career_categories cc ON cp.career_category_id = cc.id
JOIN tripoli_schools_development.campuses c ON c.id = cp.campus_id
JOIN tripoli_schools_development.schools s ON s.id = c.school_id
WHERE cp.enabled = true and c.enabled = true and s.enabled = true and cc.id = 2
group by s.id order by s.name
これが私のモデルです:
class CampusProgram < TripoliSchool
belongs_to :career_category
belongs_to :campus
end
class CareerCategory < TripoliSchool
has_many :career_programs
has_many :campus_programs
end
class Campus < TripoliSchool
self.table_name = 'campuses'
belongs_to :school
has_many :campus_programs
end
class School < ActiveRecord::Base
has_many :campuses, :class_name => "Campus"
has_many :campus_programs, :through => :campuses
end
そしてすべての重要な
class TripoliSchool < ActiveRecord::Base
self.abstract_class = true
establish_connection "tripoli_schools_#{Rails.env}"
end