# Issue with using has\_many

**URL:** <https://www.ruby-forum.com/t/issue-with-using-has-many/113801>\
**Category:** Rails\
**Created:** [September 5, 2007, 5:47am UTC](https://www.ruby-forum.com/t/issue-with-using-has-many/113801 "2007-09-05T05:47:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [September 5, 2007, 5:47am UTC](https://www.ruby-forum.com/t/issue-with-using-has-many/113801/1 "2007-09-05T05:47:56Z")

</div>

I"m getting information for a Gm and his players through a tEam table.

The relationship looks like this.

Gms -\> teams \<- nhl\_stats

So teams is the join / lookup table with FK’s to gm.id and nhl\_stats\_id

class Gm \< ActiveRecord::Base  
has\_many :teams  
has\_many :players, :through =\> :teams, :source =\> :nhl\_stat, :order =\>  
:player\_name  
end

class Team \< ActiveRecord::Base  
belongs\_to :gms  
belongs\_to :nhl\_stats  
end

class NhlStat \< ActiveRecord::Base  
has\_many :teams  
has\_many :gms, :through =\> :teams, :source =\> :gms  
end

I have valid data in all tables.

From the script/console:

> > gm = Gm.find(1)

=\> #\<Gm:0xb72b548c @attributes={“last\_modified”=\>“0000-00-00 00:00:00”,  
“pool\_id”=\>“1”, “id”=\>“1”, “created”=\>“0000-00-00 00:00:00”,  
“first\_name”=\>“Test”, “password”=\>“foobar”, “last\_name”=\>“User”,  
“user\_name”=\>“John”, “email\_address”=\>“removed\_email\_address@domain.invalid”}\>

> > gm.players  
> > ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not  
> > find the source association(s) :nhl\_stat in model Team. Try 'has\_many  
> > :players, :through =\> :teams, :source =\> '. Is it one of :gms or  
> > :nhl\_stats?

I have read and -reread the ActiveRecord documentation and cannot for  
the life of me see what I"m doing wrong.

Any help would be appreciated.

Thanks.

# please note I have tried ‘gm.players’ line with gm.players.each() {

|p| puts “player: #{p.player\_name}” } just to be thorough and it threw  
the same error.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [September 5, 2007, 5:53am UTC](https://www.ruby-forum.com/t/issue-with-using-has-many/113801/2 "2007-09-05T05:53:16Z")

</div>

On 9/5/07, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## |p| puts “player: #{p.player\_name}” } just to be thorough and it threw the same error.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

At a really quick glance it looks like you have a typo.  
:nhl\_stat in

has\_many :players, :through =\> :teams, :source =\> :nhl\_stat, :order =\>  
:player\_name

should be

:nhl\_stats

This is untested  
HTH  
Daniel

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [September 5, 2007, 5:59am UTC](https://www.ruby-forum.com/t/issue-with-using-has-many/113801/3 "2007-09-05T05:59:37Z")

</div>

Daniel ----- wrote:

> At a really quick glance it looks like you have a typo.  
> :nhl\_stat in
> 
> has\_many :players, :through =\> :teams, :source =\> :nhl\_stat, :order =\>  
> :player\_name
> 
> should be
> 
> :nhl\_stats
> 
> This is untested  
> HTH  
> Daniel

Getting somewhere, now when I fix typo…

> > gm = Gm.find(1)  
> > =\> #\<Gm:0xb721548c @attributes={“last\_modified”=\>“0000-00-00 00:00:00”,  
> > “pool\_id”=\>“1”, “id”=\>“1”, “created”=\>“0000-00-00 00:00:00”,  
> > “first\_name”=\>“Test”, “password”=\>“1234”, “last\_name”=\>“User”,  
> > “user\_name”=\>“John”, “email\_address”=\>“removed\_email\_address@domain.invalid”}\>
> 
> > gm.players  
> > NameError: uninitialized constant Gm::NhlStats

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [September 5, 2007, 6:08am UTC](https://www.ruby-forum.com/t/issue-with-using-has-many/113801/4 "2007-09-05T06:08:16Z")

</div>

On 9/5/07, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > should be  
> > =\> #\<Gm:0xb721548c @attributes={“last\_modified”=\>“0000-00-00 00:00:00”,  
> > “pool\_id”=\>“1”, “id”=\>“1”, “created”=\>“0000-00-00 00:00:00”,  
> > “first\_name”=\>“Test”, “password”=\>“1234”, “last\_name”=\>“User”,  
> > “user\_name”=\>“John”, “email\_address”=\>“removed\_email\_address@domain.invalid”}\>
> > 
> > > gm.players  
> > > NameError: uninitialized constant Gm::NhlStats
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

hmmm not sure on this one. Try putting in a classname in there

has\_many :players, :through =\> :teams, :source =\>  
:nhl\_stats, :class\_name =\> “NhlStat”, :order =\>  
:player\_name

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [September 6, 2007, 3:39am UTC](https://www.ruby-forum.com/t/issue-with-using-has-many/113801/5 "2007-09-06T03:39:26Z")

</div>

Daniel ----- wrote:

> On 9/5/07, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > should be  
> > > =\> #\<Gm:0xb721548c @attributes={“last\_modified”=\>“0000-00-00 00:00:00”,  
> > > “pool\_id”=\>“1”, “id”=\>“1”, “created”=\>“0000-00-00 00:00:00”,  
> > > “first\_name”=\>“Test”, “password”=\>“1234”, “last\_name”=\>“User”,  
> > > “user\_name”=\>“John”, “email\_address”=\>“removed\_email\_address@domain.invalid”}\>
> > > 
> > > > gm.players  
> > > > NameError: uninitialized constant Gm::NhlStats
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> hmmm not sure on this one. Try putting in a classname in there
> 
> has\_many :players, :through =\> :teams, :source =\>  
> :nhl\_stats, :class\_name =\> “NhlStat”, :order =\>  
> :player\_name

Alrighty here’s what the problem was. It was actually in the definition  
of the model I was using as the join.

I originally had:

class Team \< ActiveRecord::Base  
belongs\_to :gms  
belongs\_to :nhl\_stats  
end

but should have had (I.E. singular symbols reference to my joined  
model/tables):

class Team \< ActiveRecord::Base  
belongs\_to :gm  
belongs\_to :nhl\_stat  
end

adding to this my ‘final’ code to show a working representation of the  
Model as a Many to Many join reference…

class Gm \< ActiveRecord::Base  
has\_many :teams  
has\_many :nhl\_stats, :through =\> :teams  
end

and

class NhlStat \< ActiveRecord::Base  
has\_many :teams  
has\_many :gms, :through =\> :teams  
end

Thusly we see a working relationship with some output…

> > gm = Gm.find(1)  
> > =\> #\<Gm:0xb72b7fd4 @attributes={“pool\_id”=\>“1”, “id”=\>“1”,  
> > “first\_name”=\>“Test”, “password”=\>“1234”, “last\_name”=\>“User”,  
> > “user\_name”=\>“John”, “email\_address”=\>“removed\_email\_address@domain.invalid”}\>  
> > gm.players.each() { |p| puts “Player: #{p.player\_name} \t G: #{p.g}\t A: #{p.a} PM: #{p.pm}\n” };  
> > ?\> false  
> > Player: Marian Hossa G: 43 A: 57 PM: 18  
> > Player: Jarome Iginla G: 39 A: 55 PM: 12  
> > Player: Marc Savard G: 22 A: 74 PM: -19  
> > Player: Slava Kozlov G: 28 A: 52 PM: 9  
> > Player: Rod Brind’Amour G: 26 A: 56 PM: 7  
> > Player: Marc-Andre Fleury G: 0 A: 3 PM: 0  
> > Player: Justin W. G: 33 A: 34 PM: -11  
> > Player: Brian Rolston G: 31 A: 33 PM: 6  
> > Player: Nathan Horton G: 31 A: 31 PM: 15  
> > Player: Ray Emery G: 0 A: 1 PM: 0  
> > Player: Manny Legace G: 0 A: 0 PM: 0  
> > Player: Michal Rozsival G: 10 A: 30 PM: 10  
> > Player: Joe Corvo G: 8 A: 29 PM: 8  
> > Player: Andrej Meszaros G: 7 A: 28 PM: -15  
> > Player: Mike Van Ryn G: 4 A: 25 PM: -5

Yet again, this list has proved highly informative.
