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 -- Posted via http://www.ruby-forum.com/.
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-- Posted via http://www.ruby-forum.com/.
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 On 5/1/06, Duane <dmorin@gmail.com> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ben Reubenstein 303-947-0446 http://www.benr75.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060501/4e5df88d/attachment.html