我正在尝试使用 Rails 3 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