loominator1970
2012-Jan-05 21:27 UTC
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! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Craig White
2012-Jan-05 22:05 UTC
Re: 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
loominator1970
2012-Jan-05 22:23 UTC
Re: Help with Nested Associations Hash in ActiveRecord query
Awesome, thanks for the help, i appreciate it! Dave On Jan 5, 3:05 pm, Craig White <craig.wh...-wmL3h9Ogt9DQT0dZR+AlfA@public.gmane.org> wrote:> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.