Forum: Ruby on Rails ActiveRecord customer model with multiple previous addresses

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.
Jason Gilstrap (Guest)
on 2006-01-11 17:06
(Received via mailing list)
I would appreciate some advice or ideas on the best way to design my
data model for keeping track of multiple addresses for one customer.
Each customer should have one active address but I want to keep
previous addresses as well.

For instance, for each customer it would be nice to see the current
address using something like Customer.current_address put also be able
to get an array of previous addresses (ones that are not marked
active) using something like Customer.previous_addresses.

Any ideas on the best way to  handle this?

class Customer < ActiveRecord::Base
end

class Address < ActiveRecord::Base
end

Thanks in advance,

-Jason Gilstrap
 removed_email_address@domain.invalid
Alex Y. (Guest)
on 2006-01-11 17:34
(Received via mailing list)
Jason Gilstrap wrote:
> Any ideas on the best way to  handle this?
acts_as_list.

More specifically:

class Customer < ActiveRecord::Base
   has_many :addresses, :order => 'position'
   def current_address
     self.addresses.last
   end
   def past_addresses
     self.addresses[0..-2]
   end
end

class Address < ActiveRecord::Base
   belongs_to :customer
   acts_as_list :scope => :customer
end

Although that'll get a little hairy if they've got more than one active
address at once (work/home, for example)...
This topic is locked and can not be replied to.