# Problems with updating multiple HABTM relationships

**URL:** <https://www.ruby-forum.com/t/problems-with-updating-multiple-habtm-relationships/177648>\
**Category:** Rails\
**Created:** [October 21, 2009, 3:53pm UTC](https://www.ruby-forum.com/t/problems-with-updating-multiple-habtm-relationships/177648 "2009-10-21T15:53:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 21, 2009, 3:53pm UTC](https://www.ruby-forum.com/t/problems-with-updating-multiple-habtm-relationships/177648/1 "2009-10-21T15:53:23Z")

</div>

have the following models  
class Address \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :address\_types, :join\_table =\>  
“address\_types\_addresses”, :foreign\_key =\> “address\_id”  
has\_and\_belongs\_to\_many :organisations

class AddressType \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :addresses, :join\_table =\>  
“address\_types\_addresses”, :foreign\_key =\> “address\_types\_id”

class Organisation \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :addresses

An organisation can have a number of different addresses (main,  
dispatch, invoice etc). And one address can belong to more than one  
organisation (to allow for different companies operating out of the  
same address), and an address can be more than one type of address (ie  
the invoice address could be the same as the dispatch address, but  
different to the main office address).

There are the appropriate join tables: address\_types\_addresses, and  
addresses\_organisations.

I have a form that allows a user to add a new address (uses Ryan  
Bates’ “Handle Multiple Models in One Form” technique from the  
Advanced Rails Recipes book.

Here is the relevant part of the Organisations model:  
after\_update :save\_addresses

def new\_address\_attributes=(address\_attributes)  
#handles the address edit form in the edit view  
address\_attributes.each do |attributes|  
addresses.build(attributes)  
end  
end

def existing\_address\_attributes=(address\_attributes)  
addresses.reject(&:new\_record?).each do |address|  
attributes = address\_attributes[address.id.to\_s]  
if attributes  
address.attributes = attributes  
else  
addresses.delete(address)  
end  
end  
end

def save\_addresses  
addresses.each do |address|  
address.save(false)  
end  
end

The problem I am having is that the save is great - except for it  
doesn’t enter a value in the address\_types\_address table. Being an  
old-fashioned PHP programmer, I am slightly struggling to find out how  
to make it update that table as well. I am guessing that since there  
is no direct relationship between address\_types and organisations,  
rails doesn’t know about it. How to tell it … Is this something  
simple, or is there a bus-sized hole in the logic?

Any help would be v gratefully received.  
Thanks  
Ben

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 22, 2009, 6:17am UTC](https://www.ruby-forum.com/t/problems-with-updating-multiple-habtm-relationships/177648/2 "2009-10-22T06:17:46Z")

</div>

Hi BenR

> “address\_types\_addresses”, :foreign\_key =\> “address\_types\_id”

```
  Is this address_types_id Or address_type_id ?

```

Sijo
