DataMapper + Rails != relationships? (with different primary/foreign keys than id...)

25 views
Skip to first unread message

taelor

unread,
Jan 20, 2009, 12:21:42 PM1/20/09
to DataMapper
Hey guys,

With the upcoming merger between Rails and Merb, I have decided to
change my blog back to Rails from Merb, but still use DataMapper
instead of ActiveRecord.

Everything has worked fairly smoothly, except one thing. I have a has
many relationship between Post and Comments. When I try and do
@post.comments, I get an error...

"Cannot find the child_model Comment for Post"


What's weird is in my controller, if I have something like...

@comments = Comment.all(:post_slug => params[:id]) #params[:id] is
the title slug, which is the primary key for post

then @post.comments works.


So I think it stems from something in my model definitions, which can
be here...

http://pastie.org/365781


Now, this code worked in Merb, and let me use the @post.comments. I
think it has to do with the fact that my primary key for my Post is
the slug, and the foreign key is called post_slug in the comments. Is
there something I need to define when I say has n, :comments or
belongs_to :post? something like child_key => "post_slug" or
something?

I think its so weird that in my contoller if I search for the
@comments, and don't even reference it, its ok to say @post.comments.
I would love to know why, just for my own knowledge.

Any help would be most appreciated. Thanks!


(BTW to give back to the community, I will write a blog about setting
DM up with Rails and this fix included)

taelor

unread,
Jan 20, 2009, 1:33:48 PM1/20/09
to DataMapper
The solution below, props to dkubb. thanks.

[12:20pm] taerb: anyone here using DM with Rails?
[12:23pm] stepheneb left the chat room.
[12:24pm] dkubb: taerb: no, but I've seen it done before. are you
having problems with it?
[12:24pm] taerb: kinda... can i give you a link to look at?
[12:25pm] dkubb: taerb: sure. did you just post on the mailing list?
[12:25pm] taerb: google groups
[12:25pm] taerb: http://groups.google.com/group/datamapper/browse_thread/thread/d4308f62a56cbc3c#
[12:25pm] dkubb: taerb: right. I saw that
[12:25pm] taerb: its so weird
[12:26pm] dkubb: taerb: as a small test, at the top of your post.rb,
can you explicitly require the comment model?
[12:26pm] taerb: sure
[12:26pm] taerb: one sec
[12:26pm] dkubb: taerb: I just want to rule out the possibility that
the model load process in Rails is borked
[12:28pm] taerb: holy crap
[12:28pm] taerb: that worked
[12:30pm] dkubb: taerb: good. at least we know what the problem was
[12:30pm] dkubb: taerb: I think it has to do with how Rails lazy loads
the models on their first access
[12:31pm] taerb: gotcha

Dan Kubb (dkubb)

unread,
Jan 20, 2009, 2:26:33 PM1/20/09
to DataMapper
taelor,

> [12:26pm] dkubb: taerb: as a small test, at the top of your post.rb,
> can you explicitly require the comment model?
> [12:26pm] taerb: sure
> [12:26pm] taerb: one sec
> [12:26pm] dkubb: taerb: I just want to rule out the possibility that
> the model load process in Rails is borked
> [12:28pm] taerb: holy crap
> [12:28pm] taerb: that worked

Can you try installing edge Extlib (http://github.com/sam/extlib),
remove the explicit require, and see if the change I made a moment ago
fixes your problem?

I made it so that Module.find_const uses the const_missing hook if the
constant is not found rather than just throwing a NameError
exception. 99 times out of 100, there won't be a const_missing hook
defined so a NameError will be thrown anyway, but I believe that is
what Rails and autoload use to dynamically load the constants on their
first use.

Dan
(dkubb)
Reply all
Reply to author
Forward
0 new messages