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
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