Re: Issue with using has_many....

Daniel N
Wed, 05 Sep 2007 03:52:28 -0000

On 9/5/07, Jean Nibee
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:
>
>
> 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"=>"jean-J0of1frlU80@public.gmane.org"}>
>
> >> gm.players
> ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not
> find the source association(s) :nhl_stat in model Team.  Try
''has_many
> :players, :through => :teams, :source => <name>''.  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.
> --
> 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