Thursday, January 5, 2012

Re: [Rails] Help with Nested Associations Hash in ActiveRecord query

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

> 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!
----
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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate