# Highlight member of has\_many association

**URL:** <https://www.ruby-forum.com/t/highlight-member-of-has-many-association/124845>\
**Category:** Rails\
**Created:** [January 3, 2008, 1:58am UTC](https://www.ruby-forum.com/t/highlight-member-of-has-many-association/124845 "2008-01-03T01:58:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![reynar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@reynar](https://www.ruby-forum.com/u/reynar)\
**Post date:** [January 3, 2008, 1:58am UTC](https://www.ruby-forum.com/t/highlight-member-of-has-many-association/124845/1 "2008-01-03T01:58:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![reynar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@reynar](https://www.ruby-forum.com/u/reynar)\
**Post date:** [January 3, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/highlight-member-of-has-many-association/124845/2 "2008-01-03T19:15:32Z")

</div>

Andrew E. 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)
