Rspec Active Record Problem

Rspec forum isn’t working

Rspec is having trouble recognizing where method.

Here’s the rspec output:

NoMethodError:
undefined method where' for #<Article:0x000000066ceb38> # /apps/rvm/gems/ruby-2.0.0-p481/gems/activemodel-4.0.10/lib/active_model/attribute_methods.rb:439:inmethod_missing’
#
/apps/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.10/lib/active_record/attribute_methods.rb:168:in
method_missing' # /apps/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.10/lib/active_record/querying.rb:9:inwhere’

The spec calls index which then calls a public method that uses the
following method:

class X < ActiveRecord::Base
def self.low_level
where(:level=> 1)
end
end

Any ideas what could be the problem?

Jan,

That is pretty strange-- but it looks like you’re doing something
non-standard and you’ve gotten yourself into a pickle.

I notice that the error message says there’s no method ‘where’ on an
instance of an Article. Normally you call where on the class itself.

First of all, why is your class named “X” in your example (did you do
that just to share you code? It’s a little confusing)?

Secondly, can you show us the calling code please?

Finally, put a debugging statement inside of self.low_level and then
type “self” to understand the context (scope) of how it is called – if
“self” inside the method is actually your Article class or (somehow) an
instance of an Article.

-Jason

Try using scope
http://guides.rubyonrails.org/active_record_querying.html#scopes.

===================
Alexandre Mondaini Calvão

Nossa recompensa se encontra no esforço e não no resultado. Um esforço
total é uma vitória completa
.” [Ghandi]

2014-09-24 12:52 GMT-03:00 Jason Fleetwood-Boldt [email protected]:

Jason Fb wrote in post #1158388:

Jan,

That is pretty strange-- but it looks like you’re doing something
non-standard and you’ve gotten yourself into a pickle.

I notice that the error message says there’s no method ‘where’ on an
instance of an Article. Normally you call where on the class itself.

Good point

First of all, why is your class named “X” in your example (did you do
that just to share you code? It’s a little confusing)?

I did that just to share code

Secondly, can you show us the calling code please?

Finally, put a debugging statement inside of self.low_level and then
type “self” to understand the context (scope) of how it is called – if
“self” inside the method is actually your Article class or (somehow) an
instance of an Article.

Good idea

-Jason

I think what [email protected] pointed out is most poignant, you can
accomplish what you are trying to accomplish with this (ruby 2 syntax)

scope :low_level, → { where(:level => 1) }

The you reference this scope as Article.low_level

as you get your hands dirty with AR scopes, read & understand what “lazy
evaluation” is in the query lifecycle.

here’s a good post about it:

On Wednesday, 24 September 2014 10:14:02 UTC-4, Ruby-Forum.com User
wrote:

/apps/rvm/gems/ruby-2.0.0-p481/gems/activemodel-4.0.10/lib/active_model/attribute_methods.rb:439:in

Any ideas what could be the problem?

There isn’t enough code here to know for sure, but something strange is
going on with your X class. Line 9 of active_record/querying.rb
delegates
the where method to all. Normally this is a class method that
returns a
Relation object. Does your X class have an all class method that’s
overriding that?

–Matt J.