Sorting on a calculated column in table-plus is quite annoying. I have searched for answers - and I haven't been able to find one....has anyone else?
So here is my solution, not DRY, but it works. It is based on the idea that someone suggested for creating a view - but I wanted to define it in the app itself.
---
In the model, where you have defined the variable like
def mycolumn
one_column - two_column
end
you can add a named_scope and inject sql like
named_scope :specialscope, {
:select => "mymodels.*, (mymodels.one_column - mymodels.two_column) as mycolumn"
}
and then in your controller you just add the named scope, and table-plus sorting options....
def index
hobo_index Mymodel.specialscope.apply_scopes(:search => [params[:search], :blah1, :blah2], :order_by => parse_sort_param(:mycolumn, :name))
end
magic! it works!
what do you guys think? is this really bad for performance etc?