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.

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

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