Highlight member of has_many association

Hi,

I am trying to work out the best way to designate a member of a
has_many relationship with a specific responsibility. In this instance
I have a customer model with many addresses, however I need to flag
one of them as the billing address.

Every customer must have a single billing address. If only one address
is present in the addresses collection, by definition this would also
be flagged as the billing address.

I am struggling to find a clean looking way of doing this. I have come
up with the following model associations. Within the user interface
you can select any member of the addresses collection and assign it as
the billing_address. On customer creation a single address is also
captured, saved and assigned to the collection and also the
billing_address field.

I assume I'll also be putting in some validation logic to ensure
billing_address is always a member of the addresses collection.

Has anyone tackled this a different way?

class Customer < ActiveRecord::Base
   belongs_to :billing_address, :class_name => "Address"
   has_many :addresses, :dependent => :destroy
end

Thanks, Andrew.

Andrew Edwards wrote:

Hi,

I am trying to work out the best way to designate a member of a
has_many relationship with a specific responsibility. In this instance
I have a customer model with many addresses, however I need to flag
one of them as the billing address.

Every customer must have a single billing address. If only one address
is present in the addresses collection, by definition this would also
be flagged as the billing address.

I am struggling to find a clean looking way of doing this. I have come
up with the following model associations. Within the user interface
you can select any member of the addresses collection and assign it as
the billing_address. On customer creation a single address is also
captured, saved and assigned to the collection and also the
billing_address field.

I assume I'll also be putting in some validation logic to ensure
billing_address is always a member of the addresses collection.

Has anyone tackled this a different way?

class Customer < ActiveRecord::Base
   belongs_to :billing_address, :class_name => "Address"
   has_many :addresses, :dependent => :destroy
end

Thanks, Andrew.

I think this is pretty much the way I would do it. The other
alternatives a) having some kind of column in addresses such as 'role'
or b) creating a third table both seem poorer then what you've got.

I would on the other hand design the customer class a little
differently:

assuming a column in table customers called billing_address_id,

class Customer < ActiveRecord::Base
  has_many :addresses,
    :dependent => :destroy,
    :after_add=>:insure_billing_address
  do #block for has_many :addresses
    def billing
      find(proxy_target[:billing_address_id])
    end
  end

  #callback for addressed:after_add. Sets the billing address to the
new
  #address if there is not yet a billing address assigned.
  def insure_billing_address(address)
    self[:billing_address_id] = address[:id] unless
self[:billing_address_id]
  end
end

This would allow something like:
  customer = Customer.find :first #etc...
  send_bill_to(customer.addresses.billing)