DataMapper

1 view
Skip to first unread message

lsiden

unread,
Feb 13, 2010, 3:56:48 PM2/13/10
to Detroit.rb
Does anyone have a little knowledge or experience (or would like to
get to know) DataMapper that could help me through a problem I am
having with a test case while developing my model?

I know that Dan Parker has done some work b/c I see some of his notes
from previous discussions, but I want to check with everyone in case
Dan is too busy.

Hope you're all keeping warm and maybe getting away a bit for a little
sunshine!

-Larry

Daniel Parker

unread,
Feb 13, 2010, 4:44:58 PM2/13/10
to rubyd...@googlegroups.com, Detroit.rb
Post your question, I may be able to answer tonight, but if not, will
be able to get to it Monday morning if you haven't gotten an answer by
then.

- daniel

> --
> You received this message because you are subscribed to the Google
> Groups "Detroit.rb" group.
> To post to this group, send email to rubyd...@googlegroups.com
> To unsubscribe from this group, send email to
> rubydetroit...@googlegroups.com

Larry Siden

unread,
Feb 13, 2010, 6:17:16 PM2/13/10
to rubyd...@googlegroups.com, Daniel Parker
The files are here: http://gist.github.com/303714

Example.rb defines a simple model with accounts, options, ledgers, and ledger-entries (the latter not involved in the example).

An account always has three ledgers associated with it: 
  • assets
  • income
  • expenses
In addition, an account may have 0 or more Options.  Each option always contains two ledgers:
  • initial costs
  • recurring costs
Thus, every ledger must belong to either an account or to an option that belongs to an account, but not both!

As you can see, I tried to set up the relationships using "has 1".  This probably isn't the right way to do it, b/c calling has(1) from each class definition creates the table columns "account_id" and "option_id" in the ledgers table, which is not what I want.  So when it tries to create a ledger, account_id is nil and it throw an exception.

I'm trying to figure out how to do this in a way that will help insure referential integrity, according to the rules I stated above about accounts, options and ledgers.  

In addition to the above, a ledger that belongs to an option should get deleted if and only if  the option that "owns" them is deleted.  The same could be said for the three ledgers owned by the account, except accounts should never be deleted.

I'm guessing right now after writing this that I can't use the "has 1" or "belongs_to" relationships and instead have to make do with simply storing the unique integer id's of each ledger in it's corresponding owner, and making sure that everything is set up right.

Let me know what you think.  I appreciate whatever advice I can get.

Larry Siden, 734-926-9614, http://umich.edu/~lsiden

The United States is a nation of laws, badly written and randomly enforced.
--Frank Zappa 1940-1993

Daniel Parker

unread,
Feb 14, 2010, 1:27:34 AM2/14/10
to Larry Siden, rubyd...@googlegroups.com
Larry,

Short answer is, I think this is a case for a polymorphic association: http://wiki.github.com/hassox/dm-polymorphic/

You'd make Ledger polymorphic:

    is :polymorphic, :owner

Ledger#owner then points to its owning Option OR Account object. Also add a

    property :name, String

to Ledger. In Account, do:

    has 1, :assets_ledger, :model => "Ledger", :via => :owner, :name => "assets"

And make more of those customized to each type of ledger needed. I'm not 100% certain on the specifics here, these are untested; but hopefully they represent something that works.

You can also add the option :dependent => :destroy to automatically delete associated records on any association -- I think. :)

- daniel parker -

"You have granted me life and steadfast love, and your care has preserved my spirit." Job 10:12
"The LORD is my chosen portion and my cup . . . indeed, I have a beautiful inheritance." Psalm 16:5-6
"Give what you can ... take nothing back!"

Larry Siden

unread,
Feb 14, 2010, 11:24:31 AM2/14/10
to rubyd...@googlegroups.com
Forgot to include the rest of the group on my reply to Dan.


Larry Siden, 734-926-9614, http://umich.edu/~lsiden

The United States is a nation of laws, badly written and randomly enforced.
--Frank Zappa 1940-1993


On Sun, Feb 14, 2010 at 11:23 AM, Larry Siden <lsi...@gmail.com> wrote:
Thanks Dan!  Looks promising.  I was a bit confused by the README so I e-mailed him.  In particular, I was concerned by the comment:

One Massive Caveat is that using this you will get n+1 calls to the db atm if you do Comments#commentable for example. It needs a proxy object if anyone cares to write one.

I can't figure out what the "db atm" is, but I want to know about it before it bites me in the ass!

How did you find out about dm-polymorphism?  Was there a link somewhere or did you know to search for something?

Larry Siden, 734-926-9614, http://umich.edu/~lsiden

The United States is a nation of laws, badly written and randomly enforced.
--Frank Zappa 1940-1993


Reply all
Reply to author
Forward
0 new messages