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

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.
92ce537e2c732b0300d9ea97f6bf3fe3?d=identicon&s=25 Andrew Edwards (Guest)
on 2008-10-05 12:43
(Received via mailing list)
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
This topic is locked and can not be replied to.