Hi guys, In my Rails app, I have three models: A, B and C, with the following relationships: - B hasMany C - C belongsTo B I would like A to "has_one" instance of B+C. By that I mean A needs to be linked to a specific instance of B AND a specific instance of C. My first guess would be to create a fourth object, say B_plus_C that would: - belongs to B - belongs to C - belongs to A Is it a correct way to do this? I am not sure why, but it feels a bit weird to have to create a model for this. Thanks a lot! PJ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 25 February 2013 18:03, PierreW <wamrewam-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hi guys, > > In my Rails app, I have three models: A, B and C, with the following > relationships: > - B hasMany C > - C belongsTo B > > I would like A to "has_one" instance of B+C. By that I mean A needs to > be linked to a specific instance of B AND a specific instance of C.You don''t need to specify the A:B relationship. Just specify A has_one C, C belongs_to A (or the other way round if you prefer) then if you have a A in @a the C object is @a.c.b. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Oh. Absolutely. Much, much better. Thanks a lot! PJ On Monday, February 25, 2013 8:20:43 PM UTC, Colin Law wrote:> > On 25 February 2013 18:03, PierreW <wamr...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org <javascript:>> > wrote: > > Hi guys, > > > > In my Rails app, I have three models: A, B and C, with the following > > relationships: > > - B hasMany C > > - C belongsTo B > > > > I would like A to "has_one" instance of B+C. By that I mean A needs to > > be linked to a specific instance of B AND a specific instance of C. > > You don''t need to specify the A:B relationship. Just specify A > has_one C, C belongs_to A (or the other way round if you prefer) then > if you have a A in @a the C object is @a.c.b. > > Colin >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/gXySWKg_CWgJ. For more options, visit https://groups.google.com/groups/opt_out.
On 25 February 2013 23:19, PierreW <wamrewam-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Oh. Absolutely. Much, much better. Thanks a lot! > > PJ > > On Monday, February 25, 2013 8:20:43 PM UTC, Colin Law wrote: >> >> On 25 February 2013 18:03, PierreW <wamr...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> > Hi guys, >> > >> > In my Rails app, I have three models: A, B and C, with the following >> > relationships: >> > - B hasMany C >> > - C belongsTo B >> > >> > I would like A to "has_one" instance of B+C. By that I mean A needs to >> > be linked to a specific instance of B AND a specific instance of C. >> >> You don''t need to specify the A:B relationship. Just specify A >> has_one C, C belongs_to A (or the other way round if you prefer) then >> if you have a A in @a the C object is @a.c.b.In addition if A has_one C then you can say A has_one b through C and then you can say @a.b, or if A belongs_to C then you use delegate so that again you can say @a.b. There is no advantage at run time to these but they may make your code simpler. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.