On Thu, Dec 1, 2011 at 8:35 PM, Robert Walker wrote:

Kad Kerforn wrote in post #1034622:
Is it wrong to use a beings_to on both side of a one-to-one
association ?

User
belongs_to :account so I have an account_id field

Account
belongs_to :owner, :class_name => 'User', :foreign_key => 'user_id'

I can get user.account and account.owner
It runs, but I wonder about any collateral effect...

thanks for your feedback
belongs_to should be on the side of a one-to-one association that
contains the foreign key (same as a one-to-many).

The other side (the side without a foreign key should use has_one NOT
belongs to.

I think I didn't thought enough before reply. =)
Good arguments, guys!


User
has_one :account

Account
belongs_to :owner, :class_name => 'User', :foreign_key => 'user_id'

Or you could use the standard conventions:

Account
belongs_to :user

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 6 of 11 | next ›
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 1, '11 at 9:59p
activeDec 2, '11 at 11:37p
posts11
users5
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase