Thursday 21 July 2011

meta_search sort_link helper and associations

It took me a while to find this, so, for my own memory I am going to quickly write this up.

I have a view that shows a table of objects (a pretty standard index view). The only issue was I want to sort on one of the columns that actually has data coming not from the main object but an association.
What I discovered is a line in a posting here that says:

You can define your own custom sort scopes. Define scopes named “sort_by__asc” and “sort_by__desc” and sort_link @search, :name will work as you might expect.

So, I have an object of 'info' defined like this:
class Info < ActiveRecord::Base
belongs_to :region
scope :contains_string, lambda {|str| where(:name.matches % "%#{str}%")}
search_methods :contains_string

scope :active, lambda{where(:active => true)}
search_methods :active, :type => :boolean
end


What I found is that you can put this at the end:
scope :sort_by_region_name, lambda{joins(:region).order("regions.name asc")}
search_methods :sort_by_region_name


and then in my view I can do:

<table>
<thead>
<tr>
<td width="25%"><%= sort_link @info_search, :name, "Info" %></td>
<td width="25%"><%= sort_link @info_search, :region_name, "Region" %></td>
<td><%= sort_link @info_search, :active %></td>
</tr>
</thead>
...body info...
</table


And in my controller:
class Admin::InfosController < Admin::BaseController
def index
search = params[:search] || {"meta_sort" => "name.asc"}

@info_search = Info.search(search)
@infos = @linfo_search.paginate(:page => params[:page]||1, :per_page => 15) # load all matching records
end
end


And presto I have an index that
a) default to sorting by name (see the first line of the index method)
b) let's me sort on an associated value

Cool!

Friday 1 July 2011

I'm sorry, but POW sucks

I have been (trying to) use Pow, and the Powder gem, for a few weeks now. At the outset it looked good: you get a local domain for you test on, you don't have to worry about deployments, etc etc.
But it sucks.
If you get an error in rendering (and we are using this in development, so of course this is going to happen) pow goes into a tail spin and on my MacPro it takes almost a minute to come out of it as it retries the request dozens of times.

So you end up with a huge log that looks pretty much like this:
  SQL (2.2ms)  SHOW TABLES
SQL (1.2ms) SHOW TABLES
SQL (0.9ms) SHOW TABLES
SQL (1.2ms) SHOW TABLES
SQL (0.9ms) SHOW TABLES
SQL (1.2ms) SHOW TABLES
SQL (1.2ms) SHOW TABLES
SQL (1.2ms) SHOW TABLES
SQL (1.0ms) SHOW TABLES
SQL (1.3ms) SHOW TABLES
SQL (0.9ms) SHOW TABLES
SQL (1.2ms) SHOW TABLES
SQL (0.9ms) SHOW TABLES
SQL (1.4ms) SHOW TABLES
SQL (1.2ms) SHOW TABLES
SQL (1.4ms) SHOW TABLES
SQL (1.1ms) SHOW TABLES
Rendered /Users/smyp/.rvm/gems/ree-1.8.7-2011.03@toygaroo_r3/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (8663.2ms)
Rendered /Users/smyp/.rvm/gems/ree-1.8.7-2011.03@toygaroo_r3/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (8728.0ms)
Rendered /Users/smyp/.rvm/gems/ree-1.8.7-2011.03@toygaroo_r3/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (22759.6ms)
Rendered /Users/smyp/.rvm/gems/ree-1.8.7-2011.03@toygaroo_r3/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (22758.7ms


I am going back to using Passenger standalone. Ok, you loose the pretty local domain, but it works much more reliably.