Forum: Ruby on Rails Re: Re: Aggregating two objects of the same type

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1ca94522746f7fcf4eea3d3f418d3a66?d=identicon&s=25 Dan Munk (Guest)
on 2006-04-05 01:59
(Received via mailing list)
>> customer to addresses where an address type is used to denote the
>    :class_name => "Address"
>end

Thanks for the response.  While this option would certainly work, I
was hoping to avoid it for two reasons:

1.  Conceptually, the address belongs to the customer.  I understand
that the belongs_to in this situation gets the keys right, but it
doesn't really express the domain.
2.  I would like to avoid having the keys in the addresses table as I
can forsee similar relationships to other objects than customers.

The other two options I have been considering are:

1.  has_and_belongs_to_many with join models
2.  Polymorphic associations.

Both seem complicated as a solution.  It seems like it would be so
much cleaner if the key for one-to-one relationship were in the parent
and not the child.

Thanks,
Dan
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-04-05 10:03
(Received via mailing list)
Dan Munk wrote:
> that the belongs_to in this situation gets the keys right, but it
> doesn't really express the domain.
If you're not comfortable with the semantics, you can do:

class Customer < ActiveRecord::Base
   alias_method :refers_to, :belongs_to
end

and use refers_to (or

> 2.  I would like to avoid having the keys in the addresses table as I
>  can forsee similar relationships to other objects than customers.
Sorry, I should have made that clear - the key is in the customers table
  with belongs_to.

>
> The other two options I have been considering are:
>
> 1.  has_and_belongs_to_many with join models 2.  Polymorphic
> associations.
>
> Both seem complicated as a solution.  It seems like it would be so
> much cleaner if the key for one-to-one relationship were in the
> parent and not the child.
It is.  From the docs:
This topic is locked and can not be replied to.