鉴于我的三个模型:
class Classroom < ActiveRecord::Base
has_many :students
has_many :slideshows
has_many :grades
class Slideshow < ActiveRecord::Base
belongs_to :classroom
has_many :grades
class Grade < ActiveRecord::Base
belongs_to :slideshow
belongs_to :classroom
我创建了一个scope
来在classroom model
中进行我需要的查询:
scope :result, lambda {
joins(:slideshows, :grades).
where("classrooms.id = slideshows.classroom_id AND slideshows.id = grades.slideshows_id ").
select("classroom.name, SUM (grades.english), SUM(grades.biology) ").
group("classroom.name ").
order("SUM(grades.english+grades.biology) DESC ")
}
问题是,在我的控制器中,我无法让这个工作。我尝试了两件事:
@variable = Classroom.result
@variable = Classroom.find_by_sql(:result)
第一个选项似乎查询有效但我无法检索结果。第二个选项关闭我的服务器。关于如何实现这一目标的任何想法?
提前致谢。
答案 0 :(得分:0)
您无法在第二个选项中传递阻止。为此你必须编写SQL查询。
@variable = Classroom.find_by_sql("Your query goes here..!!")