# Has\_many :through association not working properly

**URL:** <https://www.ruby-forum.com/t/has-many-through-association-not-working-properly/189270>\
**Category:** Rails\
**Created:** [May 27, 2010, 7:43pm UTC](https://www.ruby-forum.com/t/has-many-through-association-not-working-properly/189270 "2010-05-27T19:43:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 27, 2010, 7:43pm UTC](https://www.ruby-forum.com/t/has-many-through-association-not-working-properly/189270/1 "2010-05-27T19:43:19Z")

</div>

ISSUE:  
The host has 3 ip addresses on two networks (#62 is repeated)  
How do I get host.networks to return only the two networks?

> > host  
> > =\> #\<NetworkHost id: 923, name: “ota-app1”, os\_id: 1, feature\_id: nil,  
> > description: nil, created\_at: “2010-05-18 19:05:49”, updated\_at:  
> > “2010-05-18 19:05:49”, ip\_id: nil, server\_id: 5, service\_tag: nil,  
> > location\_id: nil, rack: nil, space: nil, net\_profile\_id: 77\>
> 
> > host.ip\_addresses.size  
> > =\> 3
> 
> > host.networks  
> > =\> [#\<Network id: 62, name: “SCTCarrier990 Production Network”, ip:  
> > “192.168.110.0”, …l\>,  
> > #\<Network id: 62, name: “SCTCarrier990 Production Network”, ip:  
> > “192.168.110.0”, …\>,  
> > #\<Network id: 63, name: “SCTCarrier990 IP Profile Network3”, ip:  
> > “10.10.110.0”, …\>]
> 
> > host.networks.size  
> > =\> 3

## networks model

class Network \< ActiveRecord::Base  
has\_many :ip\_addresses, :dependent =\> :destroy  
has\_many :network\_hosts, :through =\> :ip\_addresses

## network\_hosts model

class NetworkHost \< ActiveRecord::Base

has\_many :ip\_addresses, :dependent =\> :destroy  
has\_many :networks, :through =\> :ip\_addresses

## ip\_addresses model

class IpAddress \< ActiveRecord::Base

attr\_accessible :name, :ip, :logical\_interface\_name\_id,  
:network\_host\_id, :network\_id, :is\_entry\_point, :is\_gateway, :mac

belongs\_to :network\_host  
belongs\_to :network

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 27, 2010, 11:16pm UTC](https://www.ruby-forum.com/t/has-many-through-association-not-working-properly/189270/2 "2010-05-27T23:16:30Z")

</div>

On Thu, May 27, 2010 at 10:41 AM, chewmanfoo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ISSUE:  
> The host has 3 ip addresses on two networks (#62 is repeated)  
> How do I get host.networks to return only the two networks?

> ## network\_hosts model
> 
> class NetworkHost \< ActiveRecord::Base
> 
> has\_many :ip\_addresses, :dependent =\> :destroy  
> has\_many :networks, :through =\> :ip\_addresses

Wouldn’t

has\_many :networks, :through =\> :ip\_addresses, :uniq =\> true

## do it?

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 27, 2010, 11:17pm UTC](https://www.ruby-forum.com/t/has-many-through-association-not-working-properly/189270/3 "2010-05-27T23:17:40Z")

</div>

Yes! that’s it! thanks!

On May 27, 4:14 pm, Hassan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
