.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
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?
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)
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)
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 `’
… 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
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”
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”
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
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”
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.
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…
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)
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.