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>

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.

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

I think what apotema@gmail.com 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: http://railsadventures.wordpress.com/2012/11/09/activerecord-lazy-scope-evaluation/

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 Jones