I have three models:
Mymood belongs_to mood
Mymood belongs_to user
User has_many mymoods
I have a function in User, like this:
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)
it all works as it should, but if I just call on its own:
I get the error above
Any guidance gratefully received!
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")
then my_moods.all.collect.... and you're good to go.
or my_moods = MyMood.where("my criteria").all
Thanks, Ray. Per your suggestion, this fixed it:
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!
Of course, as I just discovered, if I'd put in
has_many :moods, :through=>:mymoods
that would have worked too