Polymorphic with multiple associations to same model


#1

Hi,

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
end

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

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

Thanks, Andrew