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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.