So far, it\’s RoR

Ruby on Rails, PostgreSQL, SVN, etc….

Archive for the ‘Ruby on Rails’ Category

Using helpers in models, views, and controllers

Posted by Chirag Patel on July 8, 2008

Using this as a guide JLHPBv2: Using Helpers in Models in Rails, you can make a single Module that can be accessed from models, views, and controllers. The helpers automatically created in app/helpers/*_helpers.rb   might also be available anywhere as long as you include them.

I decided to make the methods in the helper class-scoped (using self below) so that the origin of the helper method is explicitly obvious in the call (eg. to_s below)

module UtilityHelper
  def self.camelcase_to_spaced(word)
    word.gsub(/([A-Z])/, " \\1").strip

class Event < ActiveRecord::Base
  include UtilityHelper
  def to_s
    UtilityHelper.camelcase_to_spaced("This an Event object")

module EventsHelper
  include UtilityHelper

class EventController < ApplicationController
  include UtilityHelper



Posted in Ruby on Rails, Uncategorized | 2 Comments »

FiveRuns TuneUp Rails Monitoring

Posted by Chirag Patel on June 19, 2008

I just installed FiveRuns TuneUp it’s great! A small header shows up on all your apps (see attachment). I run it locally only

It’s drop dead easy to install:

  • To add TuneUp to your Rails application, install the gem:

sudo gem install fiveruns_tuneup

  • Then, for a Rails 2.0.x app, run:

fiveruns_tuneup /path/to/rails/app

  • For a Rails 2.1 app, update your environment.rb to include this line:

config.gem ‘fiveruns_tuneup’

  • Launch your Rails app and you’ll see the header to sign up for for a free account view the stats.

Posted in Ruby on Rails | Leave a Comment »

Rails vs PHP vs Java vs …

Posted by Chirag Patel on January 23, 2008

Good sites comparing Ruby on Rails with other frameworks


Java to Rails:

Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite


PHP to Rails: » switches to Rails


To be continuted….

Posted in Ruby on Rails | Leave a Comment »

User profiles with restful_authentication

Posted by Chirag Patel on December 22, 2007


Error when updating the user from a form

ActiveRecord::RecordInvalid (Validation failed: Password confirmation can’t be blank, Password is too short (minimum is 4 characters), Password can’t be blank):





It’s probably because of the restrictions on the User model (attr_accessible or attr_protected, in particular). But allowing users access to any part of the User model via mass assignment is dangerous. That’s why many people create a separate Profile model and link it via a 1:1 (belongs_to, has_one) relationship.




Coming soon



Posted in Ruby on Rails | 2 Comments »

Linux commands to install ImageMagick and RMagick

Posted by Chirag Patel on December 19, 2007

This is what I had to do to get it to work on CentOS 5. For some reason, FreeType would not work with yum so I built it from scratch. I’m not sure if the all the “yum install”s were needed, but I did it anyway.

#yum install freetype — did not work

cd /usr/local/src


tar xvzf freetype-2.3.5.tar.gz



make install


yum install glib
yum install glib2
yum install libpng
yum install libjpeg
yum install libtiff
yum install ghostscript
yum install freetype


cd /usr/local/src


tar xvzf ImageMagick.tar.gz

cd ImageMagick-X.Y.Z

./configure –disable-static –with-modules –without-perl –without-magick-plus-plus –with-quantum-depth=8 CFLAGS=-fPIC


make install



tar zxvf RMagick-1.15.11.tar.gz

cd RMagick-1.15.4


export LD_LIBRARY_PATH=/usr/local/lib



make install



#test Rmagick in Rails

root# ruby script/console

irb(main):001:0> require ‘RMagick’

irb(main):002:0> include Magick

=> Object

irb(main):003:0> img = “test.jpg”

=> [test.jpg JPEG 10×11 DirectClass 8-bit 391b]

irb(main):004:0> img.write “test.png”

=> [test.jpg=>test.png JPEG 10×11 DirectClass 8-bit]



Posted in Linux, Ruby on Rails | Leave a Comment »

“Multiple default values” migration error with PostgreSQL 8.2.5

Posted by Chirag Patel on December 16, 2007

PostgreSQL 8.2.5 error

== CreateHeartrates: migrating ================================================

— create_table(:heartrates)

NOTICE: CREATE TABLE will create implicit sequence “heartrates_id_seq” for serial column “”

rake aborted!

PGError: ERROR: multiple default values specified for column “id” of table “heartrates”

: CREATE TABLE heartrates (“id” serial primary key DEFAULT NULL, “user_id” integer DEFAULT NULL, “timestamp” timestamp with time zone DEFAULT NULL, “heartrate” smallint NOT NULL)




I don’t see the above error on my Lenovo Windows machine because it’s running PostgreSQL 8.2.4. The issue occurs with PostgreSQL 8.2.5


Add this to the create table migration


t.column :id, :primary_key, :null => false



This is probably the simplest way to deal with this defect.



Post from: Migrations and PostgreSQL Primary Keys) – Rails Trac – Trac:

I tried to find a good place to patch the code, but everywhere I did, I could easily imagine someone yelling, “you can’t touch that”!


For instance, if scheme_definitions.rb’s primary_key could set :null => false, then everyone should be happy, even other db’s. Except, that of course you change all db’s to ‘NOT NULL’ versus ‘DEFAULT NULL’. I don’t know why you’d want to say ‘DEFAULT NULL’ though, it seems vaguely wrong.


This seems to be still in rails 2.0 too, and is still in 1.2.6.


Posted in PostgreSQL, Ruby on Rails | Leave a Comment »

Keep getting kicked off #rubyonrails IRC channel?

Posted by Chirag Patel on October 3, 2007

This is what I did when I was trying to connect to #rubyonrails channel and kept getting thrown into #overflow.

I executed the following command twice (while I was still in #overflow)

/msg nickserv register <your-password>
The first time I ran it, I got the following message:

*** -NickServ- Please wait 120 seconds before using REGISTER again

I waited a while and got this after running it a second time

:*** -NickServ- Your nickname is now registered with the password [<my-password> ].

Last, I ran the following command for good measure:

/msg nickserv register <your-password>

And got the following result:

*** -NickServ- You have already identified

Posted in Ruby on Rails | Leave a Comment »

What exactly does the scaffold_resource generator give you?

Posted by Chirag Patel on September 25, 2007

The regular scaffold generator (e.g. ruby script/generate scaffold model controller) requires you to create your model and controller first. The scaffold_resource generator, on the other hand, generates the (RESTful) model and controller for you. For example:

ruby script/generate scaffold_resource Caregiver first_name:string last_name:string

In Rails 2.0, use resource instead:

ruby script/generate scaffold Caregiver first_name:string last_name:string

Here’s everything you get:

Views for all the CRUD operations and a layout

create app/views/caregivers/index.rhtml

create app/views/caregivers/show.rhtml

create app/views/caregivers/new.rhtml

create app/views/caregivers/edit.rhtml

create app/views/layouts/caregivers.rhtml


create app/models/caregiver.rb

Controller with methods for CRUD operations

create app/controllers/caregivers_controller.rb


create app/helpers/caregivers_helper.rb

Looks like this initially

module CaregiversHelper


Migration to create the table in the database

create db/migrate/003_create_caregivers.rb

Looks like this:

class CreateCaregivers < ActiveRecord::Migration

  def self.up

    create_table :caregivers do |t|

      t.column :first_name, :string

      t.column :last_name, :string

      t.column :address, :string

      t.column :city, :string

      t.column :state, :string

      t.column :home_phone, :string

      t.column :work_phone, :string

      t.column :cell_phone, :string

      t.column :relationship, :string

      t.column :email, :string


  end  def self.down

    drop_table :caregivers



Sets up route in config/routes.rb for REST

map.resources :caregivers

Posted in Ruby on Rails | 1 Comment »

Passing parameters to partials

Posted by Chirag Patel on September 12, 2007

Partial basics

Some different things about passing parameters to partials

  1. Basics of passing parameters (i.e. locals) into partials – Why are parameters for partials (locals) passed differently than params for urls?!? Don’t know, but basically use the locals hash in both the render call and use the name of the local in the partial itself. This link also shows how to explicitly check for local parameter in case it’s nil
  2. Making a partial use optional parameters – Basically, set the parameter to nil inside the partial (e.g. title = nil unless defined?(title))
  3. Why passing locals in partials is preferred over @variables – Partials can see the @variables of its parent view, but instead pass the variables as parameters for clarity in complex code bases

Debugging partials

  1. be sure to look at console log (and/or development log), it will show the parameters being passed

Posted in Ruby on Rails | Leave a Comment »

Setting up BackgroundDrb

Posted by Chirag Patel on September 12, 2007

I decided to use BackgrounDrb 0.2.1 to launch a periodic task that would run every 15 seconds.Warning: After going through the hassle of setting up BackgrounDrb, I found out that it isn’t even supported in Windows (my dev environment)! See step 8. So, these instructions haven’t been fully tested yet on a *.nix environment. Good luck and please post comments if you have any helpful suggestions.

  1. Install BackgroundDrb: svn co backgroundrb
  2. Setup BackgroundDrb: rake backgroundrb:setup
  3. Generate worker thread (Foo is capitalized): ruby script/generate worker Foo
  4. In one of your controllers, create new worker with Middleman object (:foo_worker is lowercase with underscore)
       MiddleMan.new_worker(:class => :foo_worker,
        :args => "Arguments used to instantiate a new HeartratepostWorker object",
  5. Instantiate Middleman object in environment.rb (you might not need this with version 0.2.1 of BackgrounDrb)
      require "drb"  DRb.start_service
      MiddleMan =, "druby://localhost:22222"
  6. Install the daemons gem if you haven’t already: gem install daemons
  7. Install the slave gem if you haven’t already: gem install slave
  8. Start the BackgroundDrb server in a separate command window: ruby script\backgroundrb start
    • If you get the error “ERROR: there is already one or more instance(s) of the program running”, delete log\
    • Windows users are out of luck! You’ll get this message: `fork': the fork() function is unimplemented on this machine. Here’s a message from Ezra (the devleoper of BackgroundDrb) about Windows support:
      • “Unfortunately that won’t cut it in this case, I wish it would. Slave does more then just fork. It uses ipc and a few other things that just don’t work yet on windows. There is a possibility it could be made to work but right now it does not, even with win32 process (gem). -Ezra Wed Nov 29 16:19:03 EST 2006”
  9. Debugging methods if things don’t work well


  1. You might want to restart the backgroundrb server after you’ve modified the *_worker.rb file to lib/workers. It could cause an error otherwise.
  2. In case you get this error when starting Mongel, copy the vendor/plugins/backgroundrb directory to a temp directory and it will work

    already initialized constant OPTIONS

  3. To set up a periodic task, set up the Middleman object this way:
            session[:job_key] = worker
              :class => :heartrate_post_worker,
              :args => "some arg to do_work",
              :job_key => :simple_schedule,
              :trigger_args => {
                :start =>,
                :end => + 10.minutes,
                :repeat_interval => 15.seconds


  1. Sending emails in the background with ActionMailer and BackgrounDRB
  2. Example using TTL (time to live)

Posted in Ruby on Rails | 2 Comments »