Friday, August 18, 2006

Ruby/Rails tricks and tips with ActiveRecord

I had a simple problem definition:
  • Write a installer application in rails which will take database username, password and database name
  • Creates brand new database
  • Populate the database with schema and reference data.
  • Updates database.yml file
  • Reinitialize the rails activerecord connection context, so that user does not have to restart the application
This is how I solved it. few steps , hopefully this will be usefull for other ppl who wants to do something similar.

# Create new database

ActiveRecord::Base.connection.create_database("rex_boy") #rex_boy new database name.

# Load new database with schema and data.
.........This you can do either by using writing Rake migrate tasks or manually write SQL's to load the schema and data, 1000's of ways to do this. I did using creating using migrations and invoking rake command from rails.

# Read and update the database.yml file with newly created database

## Read the Yml and update with new database name
@config = YAML::load("#{RAILS_ROOT}/config/database.yml")) @config['development']['database']= "rex_boy"

## Now update the yml file back. basically write the database.yml back from @config object "#{RAILS_ROOT}/config/database.yml", 'w' ) do |f| f << @config.to_yaml

# Now the interesting part to reintialize ActiveRecord connection context without restarting the server (Apache or WEBrick or Lighttpd..)

ActiveRecord::Base.configurations = @config ActiveRecord::Base.establish_connection ActiveRecord::Base.connection

This should get you very fancy in terms of building rails based installer application.

Thursday, August 10, 2006

Framework comparison

Check this video (almost 340MB) out, Its fun and gives users a quick glance at all the frameworks that author of this video is trying to use. He is comparing J2EE vs Ruby on Rails vs Zope vs TurboGears vs Django.

Friday, August 04, 2006

Rails based framework for Java. Co-existence or contender?

I was sailing thru the net to see if there is anyone doing Rails framework on Java?. so some of the following guys who are doing few things similar to rails, not all.

# Trails: domain driven development framework in the spirit of Ruby on Rails or Naked Objects. Uses Spring, Tapestry, and Hibernate. Probably this one has a long way to go. Not too much happening in this area.
# Sails: Purely framework designed by keeping Rails in mind. Have a download section.It would be interesting to download and play with it.
# Grails: Sounds the closest one to rails, including controller/model generation, scaffolding etc, its written in Groovy
# JBoss Seam: Probably most active and adopted project as compared to above. Seam uses EJB3 and Hibernate (both, I wonder why?. Probably things around Transaction support and things like heavy weight vs light weight components usage).

I will keep watching this space and add if i run into any other puppy who wants to be or do things like rails in Java.