我注意到在大多数范围内你通常会传递where子句或lambda。但是我正在尝试做这样的事情而且它不会起作用
Lesley Grade
------------
id
first
last
stc_term_gpa
term
仅供参考,这是我的表:
LesleyGrade.term_gpa
当我打电话给 LesleyGrade Load (27.9ms) SELECT STC_TERM_GPA, max(TERM), max(last), max(first) FROM "lesley_grades" GROUP BY STC_TERM_GPA ORDER BY max(first), max(term) ASC
NoMethodError: undefined method `call' for #<LesleyGrade::ActiveRecord_Relation:0x007fdf9ab13d08>
from /Users/garytsai/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation/delegation.rb:136:in `method_missing'
时,这就是我得到的:
SELECT A.sc_title, A.sc_date, A.sc_coursecode, A.sc_courseno, A.sc_courseyear
FROM test_course A
INNER JOIN (SELECT min(Sc_date) SC_date, sc_title, sc_CourseCode, sc_courseno, Sc_courseYear
FROM test_Courase
where sc_date>=now()
GROUP BY sc_title, sc_CourseCode, sc_courseno, Sc_courseYear) B
on B.SC_date = A.Sc_date
and B.sc_title = A.Sc_Title
and B.Sc_CourseCode = A.SC_courseCode
and B.Sc_CourseYear = A.SC_CourseYear
WHERE A.sc_title LIKE 'Open Water Diver%'
有没有人对此有任何解决方案?
答案 0 :(得分:4)
范围正文必须包含在proc或lambda中,因此只需将其更改为:
scope :my_scope, -> {
select('STC_TERM_GPA, max(TERM), max(last), max(first)').group('STC_TERM_GPA').order('max(first), max(term) ASC')
}