The 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 look at some of what’s new installation-wise, as well as how well DataMapper is handling relationships between our model objects.
First of all, the DataMapper crew has changed to using DataObjects.rb for its database driver stuff. If you’re running OSX Leopard and MySQL as we are here at Bust Out, then you’ll want to visit this excellent post to help you get up and running.
Two addendums to the above. The first is that I needed to install the json_pure gem as well in order to get DataMapper running correctly:
sudo gem install json_pure
Secondly, if you happen to encounter something like this little gem when firing up your Rails console:
dyld: NSLinkModule() error
dyld: Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.
15.dylib
Referenced from: /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/
mysql.bundle
Reason: image not found
Trace/BPT trap
you can fix it using the following suggestion from the DataMapper mailing list.
Now that we hopefully have our new version of DataMapper correctly installed, lets take a look at it in action. Last time around, I made myself a drink to celebrate the successful install of DataMapper. This time, let’s continue building our restaurant by including a bar and then invite some people over to discuss the relative merits of ORM design patterns (or not):
>> r = Restaurant.new
=> #
>> r.name = "Hawaiian BBQ"
=> "Hawaiian BBQ"
>> r.save
=> true
>> b = Bar.new
=> #
>> b.name = 'Hula Lounge'
=> "Hula Lounge"
>> b.restaurant = r
=> #
>> b.save
=> true
>> b.restaurant
=> #
>> p = Patron.new
=> #
>> p.first_name = "jeff"
=> "jeff"
>> p.last_name = "lin"
=> "lin"
>> p.save
=> true
>> r.patrons << p
=> [#
]
>> r.save
=> nil
>> r.patrons
=> [#
]
>> p = Patron.find 1
=> #
>> p.restaurant
=> #
>> d = Drink.new
=> #
>> d.name = "Trumer Pils"
=> "Trumer Pils"
>> b.drinks << d
=> [#]
>> b.save
Hopefully at this point you get the idea that associations are working quite nicely in DataMapper (assuming any of the above is remotely helpful…wasn’t the most fun to type). So, rather than show you the model classes and keep typing along in the console, I suggest that DataMapper users read the specs that are bundled with the source. They’re quite informative and a good example of how to write specs as well.
Be sure to check out the DataMapper site as well and look for ways to contribute.
Cheers!


1 Comment
Mandarin Soda » Blog Archive » Upgrading MySQL on Leopard
—
[...] with the Ruby DataMapper ORM, upgrading is made a bit more interesting. However, the folks over at Bust Out Solutions wrote a post dealing with the simultaneous occurrence of DataMapper 0.2, Leopard and MySQL [...]