Help with Nested Associations Hash in ActiveRecord query

Hello,

Perhaps someone knows the answer to this one, because its been
stumping me for a long time.

BTW, i’m still using rails 3.0.0, perhaps this issue is solved in a
recent release.

Here are my Models

class Team < ActiveRecord::Base
has_many :players

end

class Player < ActiveRecord::Base
belongs_to :team
has_many :jerseys

end

class Jersey < ActiveRecord::Base
belongs_to :player

end

In the console (or in any of my methods), I would like to list all the
Teams that have Players with Jerseys that are XL or higher

IF I JUST WANT A COUNT OF THE TEAMS, THIS WORKS

console > Team.includes(:players => :jerseys).where(“jerseys.size in
(?)”,[‘XL’,XXL’]).count
console > 15

HOWEVER, WHEN I WANT TO GET A COLLECTION IT FAILS WITH THE FOLLOWING

MESSAGE
console > Team.includes(:players => :jerseys).where(“jerseys.size in
(?)”,[‘XL’,XXL’]).all

error message I receive

“ActiveRecord::ConfigurationError: Association named ‘jerseys’ was not
found; perhaps you misspelled it?”

What am i doing wrong? This seems like a bug, doesn’t it??

Any help is appreciated!

On Jan 5, 2012, at 2:27 PM, loominator1970 wrote:

(?)",[‘XL’,XXL’]).count

What am i doing wrong? This seems like a bug, doesn’t it??

Any help is appreciated!


clearly not a bug… Just off the top of my head, this looks more like a
failure to associate.

class Team
has_many :players
has_many :jerseys, :through => :players
end

but I think in the end, you will find that using scopes is very handy

Craig

Awesome, thanks for the help, i appreciate it!

Dave

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs