Ketan's Musings

Where he blogs about his eclipse musings

Archive for the ‘ruby’ tag

Using Models In Rails Migrations

with 2 comments

It is quite tempting to use models in rails migrations.

At the time the migration is expected to run, the model class will have been updated already, so it is hard use that in the migration itself, even though it would be useful.

Consider this example:

class CreateUsers < ActiveRecord::Migration
    def self.up
      t.string :email, :null => false
      t.string :password, :null => false
    end
 
    # create a dummy user - with newer rails versions this should really go in seeds.rb!
    User.create!(:email => 'user@example.com', :password => 'demo')
  end
 
  def self.down
    drop_table :users
  end
end

Doesn’t get simpler than this!

Now lets add another migration:

class AddSSNForUser < ActiveRecord::Migration
  def self.up
    add_column, :users, :ssn, :string, :null => false
  end 
  def self.down
    remove_column, :users, :ssn
  end
end

And a corresponding validation to the User model:

class User < ActiveRecord::Base
  # ...
  validates_presence_of :ssn
  # ...
end

Now if you were to run the database migrations on an empty database, the first migration would fail because of the lack of a SSN. Worse, it is quite possible that the User class is now called something else.

How do you get around this issue:

class CreateUsers < ActiveRecord::Migration
  # define your own User class
  class User < ActiveRecord::Base
    #...
  end
 
  def self.up
    # clear all cache that rails maintains about the User class/table mapping
    User.reset_column_information
    create_table :users do |t|
      # whatever we did in the example above
    end
 
    # this statement will always work no matter what.
    User.create!(:email => 'user@example.com', :password => 'demo')
  end
 
  def self.down
    drop_table :users
  end
end

Sometimes using SQL for performing data migrations could get quite cumbersome and unreadable, and using model objects is the simplest way to run any form of data migration. In such a case, it’s better to copy your model code into the migration.

Written by Ketan

November 20th, 2010 at 9:55 pm

Posted in rails,ruby

Tagged with ,

Code Complexity Visualization for Ruby

with one comment

Only Valid Measure of Code Quality

Only Valid Measure of Code Quality

Image from http://www.osnews.com/story/19266/WTFs_m

WTF implies lack of clarity. Clear code is easier to understand, easier to maintain and easier to extend.

Announcing saikuro_treemap — an easy to setup tool to generate complexity treemaps of ruby code.

See a demo for yourself.

Complexity Visualization of Rake

Written by Ketan

July 20th, 2010 at 8:33 pm

Posted in Uncategorized

Tagged with , ,

Controller Testing in Active Scaffold

with one comment

I just started my adventure with rails a few nights ago. Figured that Active Scaffold based controllers do something that is different from the default controllers generated by the scaffold generator.

For one all controller tests broke when I moved to active scaffold. Here’s a blog post that talks about testing active scaffold based controllers.

Written by Ketan

December 27th, 2007 at 11:40 pm

Making Eclipse Plug-ins using JRuby or Groovy

without comments

Read more about using JRuby or Groovy to write eclipse plugins here: http://dev.eclipse.org/blogs/wayne/2007/07/26/making-eclipse-plug-ins-using-jruby-or-groovy/

Written by Ketan

July 27th, 2007 at 10:49 am

Reminder: ThoughtWorks Master Class Series 2007 – Registrations closing… and fast

without comments

Sidu has written a good post on why you ought to be attending the ThoughtWorks Master Class Series 2007.

Registrations for the Bangalore event have already come to a close down. We’re already sending invites for the Pune event. There’s still some invites left for the Pune event, and you can just about make it if you register quick.

Written by Ketan

May 18th, 2007 at 2:44 pm