David Kahn wrote in post #967449:
I am getting this error on an rspec test:
undefined method `loaded?’ for #Array:0x126a4c
When I call:
You aren’t testing what you think you are…
.all returns an array even if it only finds a single instance, and at
last check, the Array class does not have a loaded? method.
Should your test be using some other method, perhaps empty?
I think I see what you are saying, but I am expecting an array — I am
asking for array#size. Method and test are copied below. The failing
is at the end of the test. The first line of the test calls the same
but it passes. I am not calling array#loaded, something in Rails must be
doing so, which is where I am stuck:
class Practice < ActiveRecord::Base
Is it ok to provide a class method practice_members when there is
already an instance method practice_members?
From my understanding they are apples and oranges. But anyway I tried out
changing PracticeMember.practice_members to
PracticeMember.global_practice_members but still get the same error.
I thought it might be worth trying. I wonder whether the stack trace
might give someone a clue.
Yeah… well this is a$$ kicking… now the weirdest part is I have an almost identical relation between Practice and User as I do between Practice and PracticeMember models, and
… give me love
Tells me what I can do with my practice members… (does the backtrace below give anyone some idea)… so I know the issues is I am blind to something and not a problem with AR or Rails (is it ever?):
ruby-1.9.2-p0 > Practice.includes(“practice_members”).all
NoMethodError: undefined method
loaded?' for #<Array:0x272aef0> from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/activerecord-3.0.3/lib/active_record/association_preload.rb:237:in preload_has_many_association’
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/activerecord-3.0.3/lib/active_record/association_preload.rb:121:in `block in preload_one_association'
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/activerecord-3.0.3/lib/active_record/association_preload.rb:115:in `each'
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/activerecord-3.0.3/lib/active_record/association_preload.rb:115:in `preload_one_association'
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/activerecord-3.0.3/lib/active_record/association_preload.rb:92:in `preload_associations'
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/activerecord-3.0.3/lib/active_record/relation.rb:68:in `block in to_a'
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/activerecord-3.0.3/lib/active_record/relation.rb:68:in `each'
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/activerecord-3.0.3/lib/active_record/relation.rb:68:in `to_a'
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/activerecord-3.0.3/lib/active_record/relation/finder_methods.rb:143:in `all'
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /Users/DK/.rvm/gems/ruby-1.9.2-p0@wavelineup3/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Meanwhile I will continue to stare at the obvious
Well doing this seemed to work.
I am finding the hard way that I should always use the model name and not plural on the has_many.
I had “has_many :practice_members” … which seems like something that Rails at least tolerated in 2x and seems to accept in 3x but is just a false sense of security. Was it always that the has_many should be singular and I just created my own superstition?
Anyhow, when I change it to “has_many :practice_member”
Then I get my love:
=> [#<Practice id: 2, name: “Davids Practice”, created_at: “2010-12-09 21:44:32”, updated_at: “2010-12-09 21:44:32”>, #<Practice id: 3, name: “Donny’s Practice”, created_at: “2010-12-09 21:52:37”, updated_at: “2010-12-09 21:52:37”>]