Chronicling My Ruby on Rails Journey

Plugin And Active Record Fun

Posted by: Bob Ngu on: March 1, 2007

My RoR efforts have culminated in JiggyMe, check it out!

First thing I tried with my own Web 2.0 app is to create a new model. In doing so, I decided that it is a good idea to add created_at, updated_at, and lock_version to every table. This is so I can leverage one of Rails Active Record cool feature. In Rails, some database attribute columns are magic columns because they get filled automatically by Rails, e.g., id, created_at, created_on, updated_at, updated_on, lock_version, etc. To avoid adding those 3 columns manually to each table, I decided to use RedHill Consulting’s Row Version Migrations plugin to handle this automatically for all table creation.

A general note about Rails plugins, unlike gems, plugins are installed directly into a specific Rails app. This makes it easier to deploy them remotely along with an entire application. Because Row Version Migrations plugin install require SVN, I also had to install SVN (apart from TortoiseSVN) in order to be able to install the plugin.

Before running any of the plugin commands below, remember to first change directory to your application root directory. If you get an HTTP error while running the command “ruby script/plugin discover”, the real culprit is likely that the plugin site is down. However, it came out as an ugly stack of errors in Ruby, there should be a better way for Ruby to handle this error because it looks like a Ruby/Rails problem initially.

C:\Documents and Settings\Bob\My Documents\user\yavis>ruby script/plugin discover
c:/ruby/lib/ruby/1.8/net/http.rb:560:in `initialize’: getaddrinfo: no address associated with hostname. (SocketError)
from c:/ruby/lib/ruby/1.8/net/http.rb:560:in `open’
from c:/ruby/lib/ruby/1.8/net/http.rb:560:in `connect’
from c:/ruby/lib/ruby/1.8/timeout.rb:48:in `timeout’
from c:/ruby/lib/ruby/1.8/timeout.rb:76:in `timeout’
from c:/ruby/lib/ruby/1.8/net/http.rb:560:in `connect’
from c:/ruby/lib/ruby/1.8/net/http.rb:553:in `do_start’
from c:/ruby/lib/ruby/1.8/net/http.rb:542:in `start’
from c:/ruby/lib/ruby/1.8/open-uri.rb:242:in `open_http’
… 13 levels…
from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/commands/plugin.rb:917
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
from script/plugin:3

First, a bit about Row Version Migrations plugin, it automatically generates the following row version columns for every table:
:created_at, :datetime, :null => false
:updated_at, :datetime, :null => false
:lock_version, :integer, :null => false, :default => 0

If you have a table for which you do not want row version columns to be generated, simply pass :row_version => false as an option to create_table:

create_table :o rders, :row_version => false do |t|

end

This plugin also requires the Redhill consulting core plugin to work, to install it first, run
“Ruby script/plugin install svn://rubyforge.org/var/svn/redhillonrails/trunk/vendor/plugins/redhillonrails_core”

Next, install Row Version Migration Plugin run
“Ruby script/plugin install
svn://rubyforge.org/var/svn/redhillonrails/trunk/vendor/plugins/row_version_migrations”

And viola, when I rake db:migrate to create the table, the new table showed up with those 3 columns added automatically, cool stuff.

1 Response to "Plugin And Active Record Fun"

I would like to see a continuation of the topic

Leave a Reply