Feb 24, 2009 / nhibernate ~ ruby
NHibernate for Ruby?

I was looking to see if there's anything like NHibernate in the Ruby world. With Ruby on Rails, ActiveRecord is the king of persistence. I thought it would be interesting to poke around at other Ruby ORMS to see what I could find.

First stop was DataMapper. I've just dipped my toe in, and there's lots to like. Most of all, how you can progress from zero-knowledge to working code in about two minutes flat.

Check out this script

require 'rubygems'
require 'dm-core'

class Post 
  include DataMapper::Resource  
  has n, :comments
  property :id, Serial
  property :title, String  
  property :created_at, DateTime
end

class Comment
  include DataMapper::Resource  
  property :id, Serial
  property :body, Text  
end

DataMapper.setup(:default, 'sqlite3::memory:')
DataMapper.auto_migrate!

post = Post.new(:title=>"This is a new post.")
post.comments << Comment.new(:body=>"Nice post!")
post.save

Just paste that into a file called: test.rb and type: ruby test.rb and it all works. Sweet.

20 lines of code to map a simple domain, connect to the database, and create the schema. Then another few lines to create a Post and add a Comment.

The best part of this story is the low barrier to entry. This is achieved through great documentation, a simple install story, and the ability to learn what you need to know very quickly and clearly.

There's lessons there to be taken back into the NHibernate community.

All that said, DataMapper is more like ActiveRecord than NHibernate. Will be interesting to take a peek at other Ruby ORMs out there...


You may also like...