I’ve been tracking progress on the DataMapper ORM framework for a little while now and was pleased to see it moved recently to a shiny new site. To celebrate the progress, I decided to write a little bit about how to get started using DataMapper in a Rails application.
The DataMapper ORM derives its name from a Martin Fowler pattern, much like its data access brethren ActiveRecord. Very simplistically, the two patterns differ primarily in regards to how much they are ORM aware, the Mapper pattern in this case ends up trading some magic for greater flexibility. Hibernate is perhaps the preeminent (or at least most popular) open source DataMapper ORM at the moment, and its very well documented in numerous books if you feel like exploring the pattern in greater depth.
With that, lets explore setting up a simple Rails application using DataMapper. To begin with, download the DataMapper gem:
sudo gem install datamapper
As a firm believer in “vendor everything”, I’m using Dr. Nic’s Gems on Rails gem to store my gems in my Rails vendor project folder. Using gems on rails (or anything similiar) is not a prerequisite to using DataMapper, but I find it makes it easier to make code tweaks (not to mention keep others you’re working with more sane in CI enviroments). If you do find yourself using the gem, you’ll need to change the following line in the datamapper init.rb file: require_options = ["datamapper"] to be ["data_mapper"].
After making those changes, go ahead and set up your database.yml file and create your database. DataMapper is able to use your database.yml. However, I had to comment out the following line in data_mapper.rb to get that working: cache WeakHash::Factory. Not exactly sure why yet, but perhaps I’ll get to that later.
Anyway, after that you need to require the following at the bottom of your environment.rb:
require 'rubygems'
require 'data_mapper'
I also turned off active_record as I’m running edge rails:
config.frameworks -= [ :action_web_service, :action_mailer, :active_record ]
Next, go ahead and define your models. In my case, I was building a restaurant application with a cocktail menu. So I created a Drink model:
class Drink < DataMapper::Base
property :name, :string
belongs_to :menu
end
After I created my model, I wanted to test it right away to see where I was at. So I fired up the Rails console. DataMapper has a nice feature that allows one to create a table through an ORM API, in this case via the following command:
>> database.save(Drink)
=> true
>> Drink.superclass
=> DataMapper::Base
So far so good, so I decided to make myself a drink:
>> d = Drink.new
=> #
>> d.name = "Nice Cold Beer"
=> "Nice Cold Beer"
>> d.save
=> 1
>> Drink.find :all
=> [#]
A simple example, but also a harbinger of things to come. The code base is seeing quite a bit of activity from the 0.1.1 version I'm on and I look forward to exploring more, particularly around advanced associations and performance. Cheers.
3 Comments
Sam Smoot
—
Wow. Thanks for the write-up Chris!
Yes, there is much activity. The soon-to-be-released 0.2.0 version is shaping up well, with *a lot* more work than the 0.1.1 version. So that’s just days away now…
From the looks of things now, 0.3.0 is probably going to be a “features” release, with a couple different forms of Migrations, and more feature coverage.
0.4.0 will probably mostly be a profiling/performance release.
At least that’s how I see it now, but I’m probably getting ahead of myself.
Bust Out Solutions » Blog Archive » DataMapper 0.2 and Rails
—
[...] DataMapper ORM framework is forging ahead and is now on version 0.2.3. In my previous DataMapper post I wrote about how to get started using the 0.1 release of DataMapper in Rails. Today, let’s [...]
leonardofaria.net → Artigos da semana sobre Rails
—
[...] Checklist – dicas básicas para implementar segurança em suas views, models e controllers. Getting Started with Rails e DataMapper – estou num meio de um projeto onde cogitamos a possibilidade de substituir o ActiveRecord pelo [...]