Issue with using has_many

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”=>“[email protected]”}>

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.

On 9/5/07, Jean N. [email protected] wrote:

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

Posted via 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

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”=>“[email protected]”}>

gm.players
NameError: uninitialized constant Gm::NhlStats

On 9/5/07, Jean N. [email protected] 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”=>“[email protected]”}>

gm.players
NameError: uninitialized constant Gm::NhlStats


Posted via 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

Daniel ----- wrote:

On 9/5/07, Jean N. [email protected] 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”=>“[email protected]”}>

gm.players
NameError: uninitialized constant Gm::NhlStats


Posted via 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”=>“[email protected]”}>
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.