I want to take advantage of rails scoping with a custom sql query. You can click this post for I'm trying to do - especially in ActiveAdmin.
How to use a find_by_sql query for an ActiveAdmin index page?
Basically I'm trying to do something like this
class YourModel < ActiveRecord::Base
scope :my_scope, where('some custom SQL')
scope :my_other_scope, where('some other custom SQL')
end
This is my query I tried out first in Rails C
LesleyGrade.where('select distinct STC_TERM_GPA,
TERM,
last,
first
from lesley_grades
order by first, term ASC')
Essentially it's a subquery in a where clause. I only want to return a distinct STC_TERM_GPA for per person per row.
These are my attributes in a lesley_grades table, which might help in understand the information I'm trying to retrieve.
id
user_id
lesley_id
last
first
active
site
cohort
section
sections_title
faculty
completed_term_cred
term
sec_start_date
sec_end_date
grade
stc_cred
active_program
most_recent_program
intent_filed
stc_term_gpa
sta_cum_gpa
start_term
prog_status
last_change_date
created_at
updated_at
So I wanted to see if this would work in Rails C first and this is what I got.
LesleyGrade Load (51.1ms) SELECT "lesley_grades".* FROM "lesley_grades" WHERE (select distinct STC_TERM_GPA,
TERM,
last,
first
from lesley_grades
order by first, term ASC)
PG::SyntaxError: ERROR: subquery must return only one column
LINE 1: ...ECT "lesley_grades".* FROM "lesley_grades" WHERE (select di...
^
: SELECT "lesley_grades".* FROM "lesley_grades" WHERE (select distinct STC_TERM_GPA,
TERM,
last,
first
from lesley_grades
order by first, term ASC)
=> #<LesleyGrade::ActiveRecord_Relation:0x3fcb44d60944>
I'm not sure how to fix
PG::SyntaxError: ERROR: subquery must return only one column
where. Also, why are you selecting four columns if you only want one?