5 Tips to Speed Up Your Rails App

9 Aug 2007

Would you like to know how you can drop the request processing time for an action from 2 seconds to 0.2 seconds? With some excellent tools, some patience, and these tips, you too can speed up critical parts of your web application.

I recently had the opportunity to help a client (and all-around good guy) improve the efficiency of the API portion of his web application. His developer had done a great job with the logic and functionality, but they thought it would be a good idea to have me take a long look at the code to see what improvements I could make. Here are five tips you can use to speed up the slow requests in your Rails app.

Read the rest of this entry »


OpenID Sample Application Updated

27 Jul 2007

Jiiim Martin wrote me to ask about the changes that have happened to the openid plugin since I created my OpenID sample Rails application back in May. He pointed out some bug fixes that have occurred to the plugin, so I have updated my sample application to include those bug fixes, and you can download it here (the same location as before).

If you’d like a little more OpenID excitement in your life, check out Dr. Nic’s OpenID sample application that supports multiple OpenID URLs per user.



User roles in Rails applications

27 Jun 2007

This morning I decided to add roles to CatchTheBest to start adding a few simple permissions to the application. Sometimes people’s eyes glaze over when they hear role-based access control, but it really doesn’t have to be complicated. There are some plugins out there that are more or less plug and play for implementing RBAC, but here’s a quick and simple example you can use in your project to easily add user roles.

First, the migration:

class CreateRoles < ActiveRecord::Migration
    def self.up
      create_table :roles do |t|
        t.column :name, :string
      end

      create_table :user_roles do |t|
        t.column :user_id, :integer
        t.column :role_id, :integer
        t.column :created_at, :datetime
      end

      add_index :roles, :name
      Role.create(:name => ‘Admin’)

    end

    def self.down
      drop_table :roles
      drop_table :user_roles
    end
  end

This adds the roles and user_roles tables and creates the first role (Admin). I have added an index to the name field of the roles table because we’ll be querying on that field quite a bit.

Here are the new models:

class Role < ActiveRecord::Base
  end
 
  class UserRole < ActiveRecord::Base
    belongs_to :user
    belongs_to :role
  end

There’s nothing terribly exciting there. :) Notice that I don’t have any relationship declarations in Role, as I currently don’t care about finding users by role — I only care about finding roles by user.

Here are the changes to the User model:

class User < ActiveRecord::Base
    has_many :user_roles
    has_many :roles, :through => :user_roles
 
    def has_role?(role)
      self.roles.count(:conditions => [‘name = ?’, role]) > 0
    end

    def add_role(role)
      return if self.has_role?(role)
      self.roles < < Role.find_by_name(role)
    end
  end

And that’s it. Easy, eh?



Personal Rails Training

14 Jun 2007

Here’s a quick update on some one-on-one Rails training that I’ve been doing. With the help of Skype and Yugma, a web-conferencing tool that actually works on the Mac, I’ve been able to offer my clients over-the-shoulder sessions as I walk them through Rails topics such as deployment and testing, or even help them build their apps as they watch along.

It’s quite fun to get to the end of the session and hear comments like “I can’t believe how much we got done”, “this will save me so much time”, and “wow, that was really cool!” It’s also cool to read the blog post of a happy Rails training client — thanks Joon!

If this sounds like something you’d like to do, get your own Ruby on Rails personal coach today! :)



Tesly Jr. Now Open Source

7 Jun 2007

A while ago I launched Tesly Jr. as a free hosted service for producing HTML reports for your automated Rails tests. A week or so before RailsConf I decided to open up the source to this simple little app, so now you can host your own version of Tesly Jr. Head on over to the Tesly Jr. site and download it from the link on the right-hand side of the page. Enjoy!