# Potential bug with named\_scope?

**URL:** <https://www.ruby-forum.com/t/potential-bug-with-named-scope/139540>\
**Category:** Rails\
**Created:** [June 4, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/potential-bug-with-named-scope/139540 "2008-06-04T21:19:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@Derek\_P](https://www.ruby-forum.com/u/Derek_P)\
**Post date:** [June 4, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/potential-bug-with-named-scope/139540/1 "2008-06-04T21:19:15Z")

</div>

Hey, I setup this named\_scope in my class:

named\_scope :latest, lambda { {:order =\> “created\_at DESC”, :limit  
=\> 1} }

it works fine, the sql returned looks like this:

…ORDER BY created\_at DESC LIMIT 1

It looks like it works perfectly, except the object returned from my  
named\_scope is actually an Array, Shouldn’t it just return the 1  
object rather than an array with 1 item? Seems like a bug to me.

---

<div class="post-metadata">

**Author:** ![Derek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@Derek\_P](https://www.ruby-forum.com/u/Derek_P)\
**Post date:** [June 4, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/potential-bug-with-named-scope/139540/2 "2008-06-04T23:51:25Z")

</div>

On Jun 4, 8:18 pm, “Derek P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> named\_scope is actually an Array, Shouldn’t it just return the 1  
> object rather than an array with 1 item? Seems like a bug to me.

I believe that it will always be like a find :all, so will return an  
array, much as find :all, :limit =\> 1 does.

Fred

---

<div class="post-metadata">

**Author:** ![Derek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@Derek\_P](https://www.ruby-forum.com/u/Derek_P)\
**Post date:** [June 5, 2008, 12:14am UTC](https://www.ruby-forum.com/t/potential-bug-with-named-scope/139540/3 "2008-06-05T00:14:17Z")

</div>

Yeah, that just seems wrong to me, because now my method chain is  
stupid long:

ie: Object.relationship.latest.first

just seems kind of lame, it’d be cool if it were smart enough to  
notice a limit = 1 means 1 element in an array and to just ignore it.

- D

On Jun 4, 2:50 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Derek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@Derek\_P](https://www.ruby-forum.com/u/Derek_P)\
**Post date:** [June 5, 2008, 1:48am UTC](https://www.ruby-forum.com/t/potential-bug-with-named-scope/139540/4 "2008-06-05T01:48:05Z")

</div>

Call me crazy, but I’m afraid I have to disagree. There is nothing in  
the syntax of the original post that indicates the results should be  
anything other than an array. Granted an array of one object, but for  
consistency if you changed the behavior of named\_scope to be “smart”  
enough to look at the SQL LIMIT then the ActiveRecord#find method  
should also be changed to recognize that as well.

So no. I say leave named\_scope as it is. I would expect named\_scope to  
return an array. Or add another explicit way to ask for an model  
rather than an array just like you do in find.

MyModel.find :all ==\> Array  
MyModel.find :first ==\> Object  
MyModel.find :last ==\> Object

Makes sense to me and would be what I expect.

name\_scope ==\> Array

This does not make sense to me. You would need something like:

named\_scope :all  
named\_scope :first  
named\_scope :last

Just my opinion. You are welcome to disagree.

---

<div class="post-metadata">

**Author:** ![Derek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@Derek\_P](https://www.ruby-forum.com/u/Derek_P)\
**Post date:** [June 5, 2008, 1:54am UTC](https://www.ruby-forum.com/t/potential-bug-with-named-scope/139540/5 "2008-06-05T01:54:31Z")

</div>

I’m afraid I have to disagree. There is nothing in the syntax of the  
original post that indicates the results should be anything other than  
an array. Granted an array of one object, but for consistency if you  
changed the behavior of named\_scope to be “smart” enough to look at  
the SQL LIMIT then the ActiveRecord#find method should also be changed  
to recognize that as well.

So no. I say leave named\_scope as it is. I would expect named\_scope to  
return an array. Or add another explicit way to ask for a model just  
like you do in find.

MyModel.find :all ==\> Array  
MyModel.find :first ==\> Object  
MyModel.find :last ==\> Object

That makes sense to me, and would be what I expect.

name\_scope ==\> Object

That does not make sense to me. You would need something like:

named\_scope :all  
named\_scope :first  
named\_scope :last

Or something like that, which would not be buried down in the SQL.

Just my opinion.

---

<div class="post-metadata">

**Author:** ![Derek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@Derek\_P](https://www.ruby-forum.com/u/Derek_P)\
**Post date:** [June 5, 2008, 1:59am UTC](https://www.ruby-forum.com/t/potential-bug-with-named-scope/139540/6 "2008-06-05T01:59:47Z")

</div>

Or how about something even better:

## In class MyObject

## def self.latest Object.find :last end

@my\_object = MyObject.latest

Simpler and does the same thing.
