Forum: Ruby on Rails Polymorphic with multiple associations to same model

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Andrew E. (Guest)
on 2008-10-05 14:43
(Received via mailing list)

Is there a recommended way to handle the following deviance from the
standard polymorphic arrangement?

Specifically the fact that SalesOrder must refer to multiple
addresses? Ignoring the polymorphic aspect I would normally just have:

belongs_to :delivery_address, :class_name => 'Address'
belongs_to :billing_address, :class_name => 'Address'

However this does tend to end up with a slightly messy address table
with mixed reference methods, and it sounds more correct for the
address to belong to 'something' rather than the other way around. Can
this be solved or is this the limit?

class Address < ActiveRecord::Base
  belongs_to :addressable, :polymorphic => true

class Customer < ActiveRecord::Base
  has_many :addresses, as => :addressable

class SalesOrder < ActiveRecord::Base
  has_one :delivery_address, as => :addressable
  has_one :billing_address, as => :addressable

Thanks, Andrew
This topic is locked and can not be replied to.