JoshK
2010-Oct-04 16:15 UTC
newbie q on activerecord - has_one and has_many on same table?
Hi I have a table where I need to identify one of the children as the "prime" child. So, its like: Parent has_many :Child has_one: Child :as :prime_child (?) or something like that? Do I put something like prime_child_id in the Parent table? Thanks. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
steve ross
2010-Oct-04 20:14 UTC
Re: newbie q on activerecord - has_one and has_many on same table?
Parent has_many :children scope :primary, where(:role => ''primary'') Or something like that. The syntax of scope, or named_scope differs depending on the version of Rails, but you get the idea... You''ll probably want to add code to make sure that only one child has the primary role for a particular parent. HTH On Oct 4, 2010, at 9:15 AM, JoshK wrote:> Hi I have a table where I need to identify one of the children as the > "prime" child. > > So, its like: > > Parent > has_many :Child > has_one: Child :as :prime_child (?) > > or something like that? Do I put something like prime_child_id in the > Parent table? > Thanks. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Oct-04 20:27 UTC
Re: newbie q on activerecord - has_one and has_many on same table?
JoshK wrote:> Hi I have a table where I need to identify one of the children as the > "prime" child. > > So, its like: > > Parent > has_many :Child > has_one: Child :as :prime_child (?) > > or something like that? Do I put something like prime_child_id in the > Parent table? > Thanks.Yes. You should be able to do class Parent < AR::B has_many :children # not :Child ! has_one :prime_child, :class_name => ''Child'' end The two associations are completely independent. The fact that they refer to the same child class and table is irrelevant. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Reasonably Related Threads
- validating both sides of a has_one relationship breaks pickle/machinist tests
- categories/recipes & books/descriptions - has_many vs has_one => id question
- Using :include with has_one vs. has_many
- (Dreaded) STI, belongs_to
- [AWDwR] Confused about has_one and has_many