I would like to have a customer that has references to two addresses, a billing address and a shipping address. In a non-rails environment I would have two keys in the customers table, billing_address_id and shipping_address_id to reference the addresses. It appears that ActiveRecord expects the parent id to reside in the child table, regardless of whether the relationship is one-to-one or one-to-many. What would the best approach be to persist this type of data structure? I would prefer to avoid a one-to-many relationship from a customer to addresses where an address type is used to denote the shipping or billing address. Thanks, Dan
Dan Munk wrote:> I would like to have a customer that has references to two addresses, > a billing address and a shipping address. In a non-rails environment > I would have two keys in the customers table, billing_address_id and > shipping_address_id to reference the addresses. It appears that > ActiveRecord expects the parent id to reside in the child table, > regardless of whether the relationship is one-to-one or one-to-many. > > What would the best approach be to persist this type of data > structure? I would prefer to avoid a one-to-many relationship from a > customer to addresses where an address type is used to denote the > shipping or billing address.Something like: class Customer < ActiveRecord::Base belongs_to :shipping_address, :foreign_key => "shipping_address_id", :class_name => "Address" belongs_to :billing_address, :foreign_key => "billing_address_id", :class_name => "Address" end -- Alex
On Apr 04, 2006, at 11:37 pm, Alex Young wrote:> Something like: > > class Customer < ActiveRecord::Base > belongs_to :shipping_address, > :foreign_key => "shipping_address_id", > :class_name => "Address" > belongs_to :billing_address, > :foreign_key => "billing_address_id", > :class_name => "Address" > endI was just typing a reply when that came through... I was going to suggest something similar but the other way round: class Customer < ActiveRecord::Base has_one :shipping_address, :class => "Address", :conditions => "address_type = ''shipping''" has_one :billing_address, :class => "Address", :conditions => "address_type = ''billing''" end Guess you can take your pick... Ashley
>Dan Munk wrote: >> I would like to have a customer that has references to two addresses, >> a billing address and a shipping address. In a non-rails environment >> I would have two keys in the customers table, billing_address_id and >> shipping_address_id to reference the addresses. It appears that >> ActiveRecord expects the parent id to reside in the child table, >> regardless of whether the relationship is one-to-one or one-to-many. >> >> What would the best approach be to persist this type of data >> structure? I would prefer to avoid a one-to-many relationship from a >> customer to addresses where an address type is used to denote the >> shipping or billing address. > >Something like: > >class Customer < ActiveRecord::Base > belongs_to :shipping_address, > :foreign_key => "shipping_address_id", > :class_name => "Address" > belongs_to :billing_address, > :foreign_key => "billing_address_id", > :class_name => "Address" >endThanks 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
Dan Munk wrote:>> class Customer < ActiveRecord::Base belongs_to :shipping_address, >> :foreign_key => "shipping_address_id", :class_name => "Address" >> belongs_to :billing_address, :foreign_key => "billing_address_id", >> :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.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:> Is it belongs_to or has_one? > > Both express a 1-1 relationship, the difference is mostly where to > place the foreign key, which goes on the table for the class saying > belongs_to.-- Alex