# Accessing has\_many objects

**URL:** <https://www.ruby-forum.com/t/accessing-has-many-objects/111182>\
**Category:** Rails\
**Created:** [August 3, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/accessing-has-many-objects/111182 "2007-08-03T16:39:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iain\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6bbea6/32.png) [@Iain\_A](https://www.ruby-forum.com/u/Iain_A)\
**Post date:** [August 3, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/accessing-has-many-objects/111182/1 "2007-08-03T16:39:20Z")

</div>

I have a the following models

CLIENT - has\_many -\> CAMPAIGNS - has\_many -\> CODES -\> has\_many -\>HITS

So obviously a client has many codes, but each is linked to a  
campaign. Now I have managed to get all the codes by using

has\_many :codes, :through=\>:campaigns

so the model looks like this

class Client \< User  
has\_many :campaigns  
has\_many :codes, :through=\>:campaigns

Now is it possible to make the client have many hits, which would  
encompass all hits on all codes over all campaigns. Or Do I have to  
loop through every campaign and every code and every hit to find all  
the hits for a client.

---

<div class="post-metadata">

**Author:** ![Iain\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6bbea6/32.png) [@Iain\_A](https://www.ruby-forum.com/u/Iain_A)\
**Post date:** [August 3, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/accessing-has-many-objects/111182/2 "2007-08-03T16:59:22Z")

</div>

Try :

has\_many :codes, :through=\>:campaigns, :include =\> :hits

then

client.codes.map(&:hits).inject(&:+)

On 8/3/07, Iain A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> so the model looks like this
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![Iain\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6bbea6/32.png) [@Iain\_A](https://www.ruby-forum.com/u/Iain_A)\
**Post date:** [August 3, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/accessing-has-many-objects/111182/3 "2007-08-03T17:00:22Z")

</div>

Actually, that’d depend on what’s exactly inside yout Hit model.

On 8/3/07, Pratik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I have a the following models  
> > class Client \< User
> 
> –  
> Cheers!
> 
> - Pratik  
> [http://m.onkey.org](http://m.onkey.org)

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![Iain\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6bbea6/32.png) [@Iain\_A](https://www.ruby-forum.com/u/Iain_A)\
**Post date:** [August 3, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/accessing-has-many-objects/111182/4 "2007-08-03T17:10:23Z")

</div>

My hit model has a month, a year and a hit count, and obv a code\_id.

what does client.codes.map(&:hits).inject(&:+) do? i.e the  
map(&:hits).inject(&:+)

---

<div class="post-metadata">

**Author:** ![Iain\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6bbea6/32.png) [@Iain\_A](https://www.ruby-forum.com/u/Iain_A)\
**Post date:** [August 3, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/accessing-has-many-objects/111182/5 "2007-08-03T17:16:24Z")

</div>

Ah. Try :

In client.rb :  
has\_many :codes, :through=\>:campaigns, :include =\> :hits

def hits  
self.codes.map(&:hits).flatten.map(&:hit\_count).inject(&:+)  
end

Then for example :  
client = Client.find(:first).hits

Explaination of  
“self.codes.map(&:hits).flatten.map(&:hit\_count).inject(&:+)” do :

- self.codes will get you all associatied codes.
- .map(&:hits) will return array of associatiated hits with codes.  
It’ll be an array of array. So we use .flatten
- map(&:hit\_count) will return array of hit\_count from all Hit objects  
in the array
- inject(&:+) is just a fancy way to do sum.

e.g. [1,2,3].inject(&:+) =\> 6

On 8/3/07, Iain A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > On 8/3/07, Iain A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > > 
> > > > so the model looks like this  
> > > > –

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![Iain\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6bbea6/32.png) [@Iain\_A](https://www.ruby-forum.com/u/Iain_A)\
**Post date:** [August 3, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/accessing-has-many-objects/111182/6 "2007-08-03T17:38:26Z")

</div>

That sounds great. Thanks Alot.
