Rspec Active Record Problem

32 views
Skip to first unread message

Jan Yo

unread,
Sep 24, 2014, 10:14:02 AM9/24/14
to rubyonra...@googlegroups.com
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:in
`method_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:in
`where'


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?

--
Posted via http://www.ruby-forum.com/.

Jason Fleetwood-Boldt

unread,
Sep 24, 2014, 11:53:39 AM9/24/14
to rubyonra...@googlegroups.com


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

Alexandre Calvão

unread,
Sep 24, 2014, 1:54:02 PM9/24/14
to rubyonra...@googlegroups.com
Try using scope.


===================
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]

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/A1755A1E-C112-4148-9B31-C2FC70BEB82C%40datatravels.com.

For more options, visit https://groups.google.com/d/optout.

Jan Yo

unread,
Sep 24, 2014, 5:19:44 PM9/24/14
to rubyonra...@googlegroups.com
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

Jason Fleetwood-Boldt

unread,
Sep 24, 2014, 5:31:09 PM9/24/14
to rubyonra...@googlegroups.com

I think what apo...@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.

Matt Jones

unread,
Sep 25, 2014, 3:51:28 PM9/25/14
to rubyonra...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages