Forum: Ruby on Rails one-to-one relationship confusion

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-08 04:38
(Received via mailing list)
Hello,

I posted a similar email, earlier in the week, but I still must not be
getting it.

I have a customer that aggregates two addresses; a shipping address
and a billing address.  Addresses can also be referenced by other
objects in a similar way, so I would really like to avoid putting the
keys referencing the customer or other objects in the addresses table.
 Addresses are exclusively referenced by parent objects and a
many-to-many association is not appropriate.

Ideally I would like the customers table to use a shipping_address_id
and a billing_address_id to reference the aggregate addresses.  For
illustration purposes, another object, company might have a
main_address_id, an after_hours_address_id and a weekend_address_id.

In order to keep the keys in the customer table the belongs_to
statements are in the customer model and the has_one statement is in
the address model.

This creates the structure I am after, but not the behavior.  The
customer is really the parent object, when I delete the customer I
want it to cascade to addresses, etc.

It seems like I am writing more code than I should to propagate
updates, etc to addresses.  Am I totally off base?  Any guidance would
be greatly appreciated.

Thanks,
Dan
89d59bd37d7bb114b192fd15d699fa15?d=identicon&s=25 Michael Johnson (Guest)
on 2006-04-08 06:24
(Received via mailing list)
One solution might be to create a many-to-many relationship for each
type of object that can have an address, but only ever put one address
in the collection. So there would be an address_customer table with
columns address_id, customer_id, and maybe address_type (indicating if
it is billing or shipping). Then you would have tables such as
address_supplier, address_employee, etc.

It's a little more work up front, but the nice thing is that this would
be very flexible for future changes.

Mike Johnson
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-04-08 08:59
(Received via mailing list)
Dan Munk wrote:
> This creates the structure I am after, but not the behavior.  The
> customer is really the parent object, when I delete the customer I
> want it to cascade to addresses, etc.
You could use a before_destroy callback in the customer for this.

class Customer < ActiveRecord::Base
   belongs_to :shipping_address...
   belongs_to :billing_address...
   before_destroy :clear_addresses
   private
     def clear_addresses
       self.shipping_address.destroy
       self.billing_address.destroy
     end
end
This topic is locked and can not be replied to.