简体   繁体   中英

Rails active record sort with a conditional. mysql

i'm trying to sort a query by two conditionals. First query is if the 'percentage' is above 75 and if it's not it will then sort by a 'grade' The percentage belongs in a another table than the grade. for example, the first query could look something like this but would only be for values above '75:

joins(:school_annual_data => :school_eligibility_data).order("GREATEST(school_annual_data.survey3_low_percent, school_eligibility_data.low_income_percent)")

Then the query would look like:

order('school_type ASC')

How can I combine them together?

You should be able to commas separate them in the same parenthesis.

.order(condition_1 ASC, condition_2 ASC)

So in your case:

.order("GREATEST(school_annual_data.survey3_low_percent, school_eligibility_data.low_income_percent), school_type ASC")

See more: http://apidock.com/rails/ActiveRecord/QueryMethods/order

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM