Forum: Ruby on Rails Getting my schema straightened out

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A24e589de07cb769a71f28aead957a48?d=identicon&s=25 Duane (Guest)
on 2006-05-01 16:47
I'm trying to map Rails to an existing schema, and having a bit of a
time of it.

I've got a table Content, and I've got a table Review, which is a type
of content.   So content has an 'id' field, and Review has a 'contentid'
field.  Not all Content has a Review, but all Reviews have exactly one
Content.

How do I represent this?  I've got a Review object, but if I say
"has_one :content" then it goes looking for a review_id key on my
content object, for which it has none.  But if I say that Content
has_one :review, and Review :belongs_to content, then all I get when
loading a Review is a nil Content object.  I think I'm probably on the
right track there, but missing a key bit of info.

Anybody able to help me unwind this?  The app I'm writing primarily
works on the Review objects (and will thus find/edit/update those), and
needs to join over to Content just for a couple of bits of meta data.

Thanks!

d
A24e589de07cb769a71f28aead957a48?d=identicon&s=25 Duane (Guest)
on 2006-05-01 16:54
I think I may have the answer to my own problem.  If my Review has_one
:content, :foreign_key => "id" then it appears to work.  Not sure if
that will unravel later down the line, but at the moment it seems to do
the job.


Duane wrote:
> I'm trying to map Rails to an existing schema, and having a bit of a
> time of it.
>
> I've got a table Content, and I've got a table Review, which is a type
> of content.   So content has an 'id' field, and Review has a 'contentid'
> field.  Not all Content has a Review, but all Reviews have exactly one
> Content.
>
> How do I represent this?  I've got a Review object, but if I say
> "has_one :content" then it goes looking for a review_id key on my
> content object, for which it has none.  But if I say that Content
> has_one :review, and Review :belongs_to content, then all I get when
> loading a Review is a nil Content object.  I think I'm probably on the
> right track there, but missing a key bit of info.
>
> Anybody able to help me unwind this?  The app I'm writing primarily
> works on the Review objects (and will thus find/edit/update those), and
> needs to join over to Content just for a couple of bits of meta data.
>
> Thanks!
>
> d
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstein (Guest)
on 2006-05-03 18:57
(Received via mailing list)
Hi D ~

To make this as painless as possible as you progress, my advice would be
to
adjust the table names to be plural and add the appropriate fields to
make
the relationships work rails style...

If this is not possible, you need to set the foreign key to a different
name
when you define the relationship like so:

has_one :tablename, :foreign_key => "your_id"

Hope this helps,

Ben
This topic is locked and can not be replied to.