Sorting on a calculated column in a table-plus

55 views
Skip to first unread message

Mike

unread,
Aug 17, 2012, 4:34:58 PM8/17/12
to hobo...@googlegroups.com
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?  

Gert Thiel

unread,
Sep 6, 2012, 8:05:48 AM9/6/12
to hobo...@googlegroups.com
My app includes myriads of calculated columns. I prefer SQL views that include these calculated columns so Rails / Hobo don't need to know about that detail.

Bryan Larsen

unread,
Sep 6, 2012, 9:53:57 AM9/6/12
to hobo...@googlegroups.com
The other option is counter_cache and similar denormalizations.
That's the approach I usually use.

Bryan
> --
> You received this message because you are subscribed to the Google Groups
> "Hobo Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/hobousers/-/EdNsvVYDslUJ.
>
> To post to this group, send email to hobo...@googlegroups.com.
> To unsubscribe from this group, send email to
> hobousers+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/hobousers?hl=en.
Reply all
Reply to author
Forward
0 new messages