Rescue from dispatching 4

Posted by pratik
on Sunday, July 20

This question gets asked quite a few times, if there’s an easy way to handle exceptions raised while dispatching a request (routing exceptions, invalid method etc.) gracefully and show a customized error page. After this commit, there is.

rescue_from to the rescue

Now you can handle those exceptions inside your application.rb like :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class ApplicationController < ActionController::Base
  rescue_from ActionController::RoutingError, :with => :route_not_found
  rescue_from ActionController::MethodNotAllowed, :with => :invalid_method
  
  private
  
  def route_not_found
    render :text => 'What the fuck are you looking for ?', :status => :not_found
  end
  
  def invalid_method
    message = "Now, did your mom tell you to #{request.request_method.to_s.upcase} that ?"
    render :text => message, :status => :method_not_allowed
  end
end

Just make sure you don’t raise another exception from your rescue_from action!

If you see this 7

Posted by pratik
on Saturday, April 12

Then you’re experiencing modrails !

So here’s your candy :

  • cp /opt/local/etc/bash_completion.d/git ~/.git_auto_complete.sh
  • mate ~/.profile
  • Add “source ~/.git_auto_complete.sh” at the very bottom
  • Open a new shell and go to your git directory
  • Enjoy tabbing

A few tiny never/rarely seen before rails 2.0 features 2

Posted by pratik
on Saturday, December 15

Asset Caching

I had talked about this before in the past. Basically, rails allows you to merge your css and javascript files in a single file which is cachable by the browsers. Hence, there’ll only be just one request ( which would return 304 response from 2nd time onwards ) per page, which would result in faster loading time for your visitors.

I tend to put these lines in my layouts :

1
2
<%= javascript_include_tag :all, :cache => true %>
<%= stylesheet_link_tag :all, :cache => true %>

Migrations now accept command line attributes

So let’s say to add a string called “name” to my users table, now I’d just do :


script/generate migration AddNameToUser name:string

And to remove a column called “useless” from users table :


script/generate migration RemoveUselessFromUser useless:string

Please note that, when you generate a migration to remove columns, attribute type may seem trivial. But it is used to generate down migration, which would re-create those columns.

Better Exception Handling

In addition to what Ryan said, you can have inline exception handlers as well.

1
2
3
rescue_from ActiveRecord::RecordNotFound
  render :file => '/bad_record', :status => 404
end

Block can even take have an argument for exception object.

1
2
3
rescue_from  ActiveRecord::RecordInvalid do |exception|
  render :action => (exception.record.new_record? ? 'new' : 'edit')
end

The above snippet would greatly DRY up your controllers if you use save! and create! in your RESTful application.

Request Profiler

Rails now includes request profiler which uses ruby-prof and generates fancy pants graphs for you. You’ll need to supply an input file to use with request profiler, which would be in your usual integration test format. A sample input file for an application using restful_authentication plugin can look somewhat like :

1
2
lifo:freeonrails pratik$ cat perfscript 
post('/sessions', { :login => 'quentin', :password => 'test' })

And then roll it like :


lifo:freeonrails pratik$ script/performance/request perfscript

And the one liners

  • render partial collection now works with a collection of hash.

<%= render :partial => 'foo', :collection => [{ :name => 'world', :address => 'bar' }, { :name => 'food', :address => 'world' }] %>
  • ActiveRecord::Base#becomes to change record’s type

render :partial => @pet.becomes(Cat) # renders cats/cat instead of pets/pet
  • link_to(:back) for “Back” button using request.env[“HTTP_REFERER”] or ‘javascript:history.back()’ ( fallback )
  • error_messages_for accepts objects so that you can display error for local variable

<%= error_messages_for 'user', :object => @question.user  %>
  • Array#rand
1
2
3
[].rand       # => nil
['a'].rand    # => 'a'
[1,2,3].rand  # => 1 or 2 or 3
  • Hash#to_query

{ :user => {:name => "lifo"} }.to_query => "user%5Bname%5D=lifo"

This post is for covering smaller fixes/features which are not very easy to spot. For very exciting and rather major Rails 2.0 features, I’d recommend you check out :

Find users with at least 'n' items 5

Posted by pratik
on Thursday, November 01

This question is asked quite a few times in #rubyonrails

When your models look like :

1
2
3
4
5
6
7
class User < ActiveRecord::Base 
  has_many :items
end

class Item < ActiveRecord::Base
  belongs_to :user
end

How do you find all the users with at least ‘n’ number of items ?

Here’s how :


User.find :all, :joins => "INNER JOIN items ON items.user_id = users.id", :select => "users.*, count(items.id) items_count", :group => "items.user_id HAVING items_count > 5"

This will give you all the users with at least 5 items.

The statement is using INNER JOIN to eliminate users with no items. Also, in :select, there is count(items.id) aliased items_count and in :group is items.user_id. This will group items by user_id and also count number of items per user. Now, database requires HAVING clause when you want to supply conditions for group functions ( items_count in our case ). ActiveRecord, as of now, doesn’t provide :having key for find(). Hence, we need to use a very little hack ( more like workaround ) to overcome that and supply HAVING clause in :group key.

May be someone interested can submit a patch for :having key in AR finders.