Has_many :through ... What am I missing?

class Ladder < ActiveRecord::Base
has_many :players
has_many :users, :through => :players
end

class Player < ActiveRecord::Base
belongs_to :users
belongs_to :ladders
end

class User < ActiveRecord::Base
has_many :players
has_many :ladders, :through => :players
end

When I try the following code (on the console) I get an error:

@ladder = Ladder.find(1)
@ladder.users

I get this error:

NameError: uninitialized constant Ladder::Users
from /opt/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/
active_support/dependencies.rb:477:in const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1360:incompute_type’
from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/reflection.rb:125:in send' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/reflection.rb:125:inklass’
from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/associations/has_many_through_association.rb:115:in
find_target' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/associations/association_proxy.rb:131:inload_target’
from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/associations/association_proxy.rb:122:in
method_missing' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/associations/has_many_through_association.rb:108:inmethod_missing’
from /opt/local/lib/ruby/1.8/irb.rb:298:in output_value' from /opt/local/lib/ruby/1.8/irb.rb:151:ineval_input’
from /opt/local/lib/ruby/1.8/irb.rb:259:in signal_status' from /opt/local/lib/ruby/1.8/irb.rb:147:ineval_input’
from /opt/local/lib/ruby/1.8/irb.rb:146:in eval_input' from /opt/local/lib/ruby/1.8/irb.rb:70:instart’
from /opt/local/lib/ruby/1.8/irb.rb:69:in catch' from /opt/local/lib/ruby/1.8/irb.rb:69:instart’
from /opt/local/bin/irb:13

What am I missing with the :through parameter? It’s driving me nuts.

-S

Hi –

On Wed, 15 Aug 2007, Stuart Grimshaw wrote:

class Ladder < ActiveRecord::Base
has_many :players
has_many :users, :through => :players
end

class Player < ActiveRecord::Base
belongs_to :users
belongs_to :ladders
end

Make those singular:

belongs_to :user
belongs_to :ladder

David

On Aug 16, 1:22 am, [email protected] wrote:

Make those singular:

belongs_to :user
belongs_to :ladder

Smashing, thanks David