# Problem with named\_scope and test-spec?

**URL:** <https://www.ruby-forum.com/t/problem-with-named-scope-and-test-spec/157819>\
**Category:** Rails\
**Created:** [January 15, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/problem-with-named-scope-and-test-spec/157819 "2009-01-15T17:23:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 15, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/problem-with-named-scope-and-test-spec/157819/1 "2009-01-15T17:23:02Z")

</div>

I may be overlooking something, but it seems to me that it is a  
problem if with this code…

Class Blog \< ActiveRecord::Base  
named\_scope :unseen, :conditions =\> “displayed IS NULL”  
end

this test fails…

it “should give a list of unseen entries” do  
Blog.expects :unseen  
b = Blog.new  
b.unseen  
end

given that the docs say about named\_scope:

“Adds a class method for retrieving and querying objects. A scope  
represents a narrowing of a database query, such as :conditions =\>  
{:color =\> :red}, :select =\> ‘shirts.\*’, :include  
=\> :washing\_instructions.  
class Shirt \< ActiveRecord::Base  
named\_scope :red, :conditions =\> {:color =\> ‘red’}  
named\_scope :dry\_clean\_only, :joins  
=\> :washing\_instructions, :conditions =\>  
[‘washing\_instructions.dry\_clean\_only = ?’, true]  
end  
The above calls to named\_scope define class methods Shirt.red and  
Shirt.dry\_clean\_only. Shirt.red, in effect, represents the query  
Shirt.find(:all, :conditions =\> {:color =\> ‘red’}).”

Am I missing something?

Thanks,  
Tom

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 15, 2009, 5:53pm UTC](https://www.ruby-forum.com/t/problem-with-named-scope-and-test-spec/157819/2 "2009-01-15T17:53:07Z")

</div>

On Thu, Jan 15, 2009 at 11:21 AM, Tom M [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> it “should give a list of unseen entries” do  
> Blog.expects :unseen  
> b = Blog.new  
> b.unseen  
> end
> 
> I don’t know if this will help or not, but the little I’ve used named  
> scopes, I’ve used them as class methods, not as instance methods, i.e.  
> Blog.unseen not b.unseen.

–wpd

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 15, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/problem-with-named-scope-and-test-spec/157819/3 "2009-01-15T19:30:18Z")

</div>

Patrick,

Thanks… I checked again and the example I posted was a bit off of my  
real example, which uses associations and scopes. According to the  
docs:

All \scopes are available as class methods on the ActiveRecord::Base  
descendent upon which the \scopes were defined. But they are also  
available to has\_many associations. If,  
class Person \< ActiveRecord::Base  
has\_many :shirts  
end  
then elton.shirts.red.dry\_clean\_only will return all of Elton’s red,  
dry clean only shirts.

So a better example would be:  
b = Blog.first  
b.entries.unseen

I guess in this case, the method :unseen is bound to the set returned  
via the association, and not the class itself?

Tom

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 16, 2009, 4:17am UTC](https://www.ruby-forum.com/t/problem-with-named-scope-and-test-spec/157819/4 "2009-01-16T04:17:05Z")

</div>

Well, I guess I/we are on our own. I’ll try to dig a bit deeper and  
see if I can find out just what’s going on w/ those associations.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 15, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/problem-with-named-scope-and-test-spec/157819/5 "2009-01-15T19:49:06Z")

</div>

On Thu, Jan 15, 2009 at 1:29 PM, Tom M [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Person \< ActiveRecord::Base  
> I guess in this case, the method :unseen is bound to the set returned  
> via the association, and not the class itself?
> 
> I’m not too sure what you mean by that. As I’ve used named scopes, I have  
> been confused by thinking that entries.unseen was an array such as is  
> returned by #find, when, it seems, it is a piece of RoR magic that looks  
> more like the model. I wonder if b.entries.unseen.all or  
> b.entries.unseen.first might do what you need it to.

(Between you and me, I’m hoping that somebody who knows what (s)he is  
talking about chimes in here pretty soon, as this is already out of my  
depth. :-))

–wpd
