Potential bug with named_scope?

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.

On Jun 4, 8:18 pm, “Derek P.” [email protected] 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

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. [email protected]

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.

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.

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.