DataMapper Association Gotcha

I've got a class that has a property that is the same name as the symbol for another class it has a "belongs_to" relationship with.

class Ledger
  include DataMapper::Resource

  property :id,Serial
  property :posted_on,Integer
  property :memorandum,String
  property :amount,Integer
  property :account_id,Integer
  property :entry_id,Integer
  property :entry_amount_id,Integer
  property :fiscal_period_id,Integer
  property :currency_id,Integer
  belongs_to :account
  belongs_to :entry
  belongs_to :entry_amount, :class_name => 'Amount', :child_key => [ :entry_amount_id ]
end

Looks like the docs at datamapper.org are a little out of sync with the version of datamapper I'm using, as they specify that something like this will work:

class Post
  include DataMapper::Resource

  belongs_to :author, :model => 'User', :child_key => [ :post_id ]
end

I scoped out my cloned copy of the dm-core code and noticed the conspicuous options:

OPTIONS = [ :class_name, :child_key, :parent_key, :min, :max, :through ]

and guessed ":class_name" might work. Indeed it does! Nice. I have to remark that working with datamapper is intellectually rewarding.

UPDATE: Issue has been logged! Lighthouse looks nice, too. I'd previously only heard of it via my work with SD.


By Albert on December 7, 2009 6:14 AM

Categories:

1 Comment

Hi

You might want to update your checkout of DataMapper, and also your dm-gems. DataMapper is currently on 0.10.1, and 0.10.2 is about to drop. There are over 100 commits between your repository and the current edge :)

In the new version of the code, :model is the correct usage, or even:

belongs_to :author, 'User', :child_key => [:post_id]