Rails 3 Active Record query returns "undefined method `loaded?' for #<Array:0x126a4c>"

I am getting this error on an rspec test:
undefined method `loaded?’ for #Array:0x126a4c

When I call:
Practice.includes(“practice_members”).all

Practice has_many :practice_members
PracticeMember belongs_to :practice

Practice.all returns:
[#<Practice id: 6, name: “Practice One”, created_at: “2010-12-09
15:40:46”,
updated_at: “2010-12-09 15:40:46”>]

PracticeMember.all returns:
#<PracticeMember id: 9, practice_id: 6, name_last: “Aaaaz”, name_first:
“Aaaba”, name_middle: “Aaabb”, created_at: “2010-12-09 15:40:46”,
updated_at: “2010-12-09 15:40:46”>, #<PracticeMember id: 10,
practice_id: 6,
name_last: “Aaabc”, name_first: “Aaabd”, name_middle: “Aaabe”,
created_at:
“2010-12-09 15:40:46”, updated_at: “2010-12-09 15:40:46”>]

So the association on practice_id is correct on both practice members.
It is
unclear to me why this should be failing… am I missing something?

Thanks,

David

David K. wrote in post #967449:

I am getting this error on an rspec test:
undefined method `loaded?’ for #Array:0x126a4c

When I call:
Practice.includes(“practice_members”).all

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?

On Thu, Dec 9, 2010 at 10:16 AM, Ar Chron [email protected] wrote:

.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
just
asking for array#size. Method and test are copied below. The failing
line
is at the end of the test. The first line of the test calls the same
method
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
def self.practice_members
# global roster, no Demo Practice members included
if PracticeMember.all.size>0
Practice.includes(“practice_members”).where(“name<>‘Demo
Practice’”).all
else
return []
end
end
end

it “can get global practice member roster not including Demo Practice”
do
assert_equal Practice.practice_members.size, 0 # this passes fine

practice_member = Factory.create(:practice_member)
practice_member.practice_id = @practice.id
practice_member.save
practice_member = Factory.create(:practice_member)
practice_member.practice_id = @practice.id
practice_member.save

assert_equal Practice.practice_members.size, 2 # this fails ... and 

note
that Practice.all returns one record with id of “6” and
PracticeMember.all
returns two records with practice_id of “6”
end

On 9 December 2010 17:07, David K. [email protected] wrote:

is at the end of the test. The first line of the test calls the same method
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
def self.practice_members

Is it ok to provide a class method practice_members when there is
already an instance method practice_members?

Colin

On Thu, Dec 9, 2010 at 11:20 AM, Colin L. [email protected]
wrote:

Practice.includes(“practice_members”).all
just
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.

By the way as far as documentation and explaining myself, I am a bit
confused about the conventions for explaining class methods:

I know that I can explain instance methods like:
PracticeMember#practice_members — is this correct, that this would
refer
to the instance and not the class method?

But how do I explain the self.practice_members method? Would it be
PracticeMember::practice_members (this looks wrong)

On 9 December 2010 17:30, David K. [email protected] wrote:

I am getting this error on an rspec test:

class Practice < ActiveRecord::Base
def self.practice_members

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.

By the way as far as documentation and explaining myself, I am a bit
confused about the conventions for explaining class methods:

I know that I can explain instance methods like:
PracticeMember#practice_members — is this correct, that this would refer
to the instance and not the class method?

But how do I explain the self.practice_members method? Would it be
PracticeMember::practice_members (this looks wrong)

Good questions.

Colin

On Thu, Dec 9, 2010 at 11:36 AM, Colin L. [email protected]
wrote:

wrote:

is at the end of the test. The first line of the test calls the same

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

Practice.includes(“users”).all

… give me love

but

Practice.includes(“practice_members”).all

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 (irb):65
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 `’

Meanwhile I will continue to stare at the obvious :slight_smile:

David

On Thu, Dec 9, 2010 at 4:15 PM, David K.
[email protected]wrote:

On 9 December 2010 17:07, David K. [email protected]

When I call:

… give me love
ruby-1.9.2-p0 > Practice.includes(“practice_members”).all
preload_associations' 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

Meanwhile I will continue to stare at the obvious :slight_smile:

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.includes(“practice_member”).where(“name<>‘Demo Practice’”).all
=> [#<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”>]

So there.

David

David K. wrote in post #967538:
[…]

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?

No. has_many should always be plural. According to the docs, this has
not changed in Rails 3.

Have you been playing around with your inflector methods?

Anyhow, when I change it to “has_many :practice_member”

Then I get my love:

Practice.includes(“practice_member”).where(“name<>‘Demo Practice’”).all
=> [#<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”>]

So there.

That makes no sense. has_many :singular shouldn’t even work.

David

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Thu, Dec 9, 2010 at 4:41 PM, Marnen Laibow-Koser
[email protected]wrote:

and I just created my own superstition?

No. has_many should always be plural. According to the docs, this has
not changed in Rails 3.

Have you been playing around with your inflector methods?

nope… generally never touch these

“2010-12-09 21:52:37”>]

So there.

That makes no sense. has_many :singular shouldn’t even work.

I know! Well, in Larry David’s words, at this point “Whatever works”. I
know
that’s not a good answer, and now I have a new supersition: has_many >>
NO
plurals.

On Thu, Dec 9, 2010 at 4:54 PM, Marnen Laibow-Koser
[email protected]wrote:

I know! Well, in Larry David’s words, at this point “Whatever works”. I
know
that’s not a good answer, and now I have a new supersition: has_many >>
NO
plurals.

Superstitions have no place in software development. You know as well
as I do that you need to find out what’s really going wrong…

Um… Im just in a gigantic skinner box most of the time

On 9 December 2010 22:26, David K. [email protected] wrote:

[…]
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?

No, it should definitely be plural.

Anyhow, when I change it to “has_many :practice_member”

Then I get my love:

Practice.includes(“practice_member”).where(“name<>‘Demo Practice’”).all
=> [#<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”>]

Well I believe that it should not work like that. Perhaps you have
found a bug.

I haven’t used includes on Rails 3 yet, but I notice that the examples
I can find use the symbol in the includes call, so
Practice.includes(:practice_members)
It might be worth trying that.

Colin

Colin

David K. wrote in post #967544:

On Thu, Dec 9, 2010 at 4:41 PM, Marnen Laibow-Koser
[email protected]wrote:

and I just created my own superstition?

No. has_many should always be plural. According to the docs, this has
not changed in Rails 3.

Have you been playing around with your inflector methods?

nope… generally never touch these

“2010-12-09 21:52:37”>]

So there.

That makes no sense. has_many :singular shouldn’t even work.

I know! Well, in Larry David’s words, at this point “Whatever works”. I
know
that’s not a good answer, and now I have a new supersition: has_many >>
NO
plurals.

Superstitions have no place in software development. You know as well
as I do that you need to find out what’s really going wrong…

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Dec 10, 2:12pm, David K. [email protected] wrote:

Ok, it is weird enough for me to spend some more time… possibly I am doing
something wrong in two places and what I have is like a double negative or
“two wrongs making a right”… I’ll report back.

Something is resulting in you having an array where you should have an
association proxy. This would happen if you had an instance method you
wrote with the same name as one of your associations (so changing the
name of the association would change what’s happening because it would
eliminate that collision)

Fred

On Fri, Dec 10, 2010 at 3:56 AM, Colin L. [email protected]
wrote:

false sense of security. Was it always that the has_many should be
singular
and I just created my own superstition?

No, it should definitely be plural.

Ok, it is weird enough for me to spend some more time… possibly I am
doing
something wrong in two places and what I have is like a double negative
or
“two wrongs making a right”… I’ll report back.

On Fri, Dec 10, 2010 at 9:49 AM, Frederick C. <
[email protected]> wrote:

Something is resulting in you having an array where you should have an
association proxy. This would happen if you had an instance method you
wrote with the same name as one of your associations (so changing the
name of the association would change what’s happening because it would
eliminate that collision)

Thanks, I think you are right. What I did was move the method to a more
appropriate model in addition to renaming. I had tried renaming
yesterday
but maybe I missed something.