I love Datamapper, but there are some aspects of it that throw me because they don’t work the way they should on first glance.
Foreign Constraints
Let’s make a few objects.
class Apple < DataMapper::Base property :name, :string end class Banana < DataMapper::Base belongs_to :apple, :class => 'Apple', :foreign_key => 'a1' belongs_to :apple, :class => 'Apple', :foreign_key => 'a2' end
And now:
>> b = Banana.find_or_create :a1 => { :name => "Red Delicious" }, :a2 => { :name => "Granny Smith" }
RuntimeError: Don't know how to quote {:name=>"Red Delicious"}
Oof. So we have to hold Datamapper’s hand the whole way.
>> b = Banana.find_or_create :a1 => Apple.find_or_create( :name => "Red Delicious" ),
:a2 => Apple.find_or_create( :name => "Granny Smith")
Updating Properties by Self-Reference
Here’s our test object.
class Apple < DataMapper::Base property :name, :string property :weight, :integer end
And now:
>> a = Apple.find_or_create :name => "Red Delicious"
>> a.update_attributes :weight => 0
So far, so good. But…
>> a.update_attributes :weight => :weight + 1
Will fail horribly. Of course,
>> a.weight = a.weight + 1
will work, but if we have a hashload of attributes we want to update, this becomes a little cumbersome.
