I have three models: (1) Mood (2) User (3) Mymood Mymood belongs_to mood Mymood belongs_to user User has_many mymoods I have a function in User, like this: def moods if mymoods.empty? [] else mymoods.collect{|mm| mm.mood} end end In Rails2, User.first.moods returns an array of moods (or an empty array where there are none) In Rails3, I get a big old error: uninitialized constant Mymood::Mood I''m guessing it''s something to do with lazy loading, because if I call (in the console) User.first.mymoods then User.first.moods it all works as it should, but if I just call on its own: User.first.moods I get the error above Any guidance gratefully received! -- 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.
Ray Parker
2010-Dec-02 08:21 UTC
Re: Lazy loading issue (I think) - what''s up with this ?
Look at things in the debugger, but the likelihood is that you''re trying to operate on what you think is an array of records, but is just a query. my_moods = MyMood.where("my criteria") my_moods.collect.... Error then my_moods.all.collect.... and you''re good to go. or my_moods = MyMood.where("my criteria").all and my_moods.collect... On Dec 1, 7:53 pm, itsterry <itste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have three models: > (1) Mood > (2) User > (3) Mymood > > Mymood belongs_to mood > Mymood belongs_to user > > User has_many mymoods > > I have a function in User, like this: > > def moods > if mymoods.empty? > [] > else > mymoods.collect{|mm| mm.mood} > end > end > > In Rails2, User.first.moods returns an array of moods (or an empty > array where there are none) > > In Rails3, I get a big old error: uninitialized constant Mymood::Mood > > I''m guessing it''s something to do with lazy loading, because if I call > (in the console) > User.first.mymoods > then > User.first.moods > it all works as it should, but if I just call on its own: > User.first.moods > I get the error above > > Any guidance gratefully received!-- 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.
Thanks, Ray. Per your suggestion, this fixed it: def moods ms=Mymood.where(''user_id=?'',user_id) if ms.empty? [] else ms.collect{|mm| mm.mood} end end But it seems strange to have to write another query in the middle of a model. I''d have thought that the "has_many :mymoods" in the user model would have been enough. Whatever. It''s working now. Thanks for your help! On Dec 2, 8:21 am, Ray Parker <rayparkerbasspla...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Look at things in the debugger, but the likelihood is that you''re > trying to operate on what you think is an array of records, but is > just a query. > > my_moods = MyMood.where("my criteria") > > my_moods.collect.... Error > > then my_moods.all.collect.... and you''re good to go. > > or my_moods = MyMood.where("my criteria").all > and my_moods.collect... > > On Dec 1, 7:53 pm, itsterry <itste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > I have three models: > > (1) Mood > > (2) User > > (3) Mymood > > > Mymood belongs_to mood > > Mymood belongs_to user > > > User has_many mymoods > > > I have a function in User, like this: > > > def moods > > if mymoods.empty? > > [] > > else > > mymoods.collect{|mm| mm.mood} > > end > > end > > > In Rails2, User.first.moods returns an array of moods (or an empty > > array where there are none) > > > In Rails3, I get a big old error: uninitialized constant Mymood::Mood > > > I''m guessing it''s something to do with lazy loading, because if I call > > (in the console) > > User.first.mymoods > > then > > User.first.moods > > it all works as it should, but if I just call on its own: > > User.first.moods > > I get the error above > > > Any guidance gratefully received!-- 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.
Of course, as I just discovered, if I''d put in has_many :moods, :through=>:mymoods that would have worked too :) On Dec 2, 9:36 am, itsterry <itste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks, Ray. Per your suggestion, this fixed it: > > def moods > ms=Mymood.where(''user_id=?'',user_id) > if ms.empty? > [] > else > ms.collect{|mm| mm.mood} > end > end > > But it seems strange to have to write another query in the middle of a > model. I''d have thought that the "has_many :mymoods" in the user model > would have been enough. > > Whatever. It''s working now. Thanks for your help! > > On Dec 2, 8:21 am, Ray Parker <rayparkerbasspla...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Look at things in the debugger, but the likelihood is that you''re > > trying to operate on what you think is an array of records, but is > > just a query. > > > my_moods = MyMood.where("my criteria") > > > my_moods.collect.... Error > > > then my_moods.all.collect.... and you''re good to go. > > > or my_moods = MyMood.where("my criteria").all > > and my_moods.collect... > > > On Dec 1, 7:53 pm, itsterry <itste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I have three models: > > > (1) Mood > > > (2) User > > > (3) Mymood > > > > Mymood belongs_to mood > > > Mymood belongs_to user > > > > User has_many mymoods > > > > I have a function in User, like this: > > > > def moods > > > if mymoods.empty? > > > [] > > > else > > > mymoods.collect{|mm| mm.mood} > > > end > > > end > > > > In Rails2, User.first.moods returns an array of moods (or an empty > > > array where there are none) > > > > In Rails3, I get a big old error: uninitialized constant Mymood::Mood > > > > I''m guessing it''s something to do with lazy loading, because if I call > > > (in the console) > > > User.first.mymoods > > > then > > > User.first.moods > > > it all works as it should, but if I just call on its own: > > > User.first.moods > > > I get the error above > > > > Any guidance gratefully received!-- 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.