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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 4, 8:18 pm, "Derek P." <de...-88kYAtoe8eYTx9n+QjaLhg@public.gmane.org> wrote:> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 4, 8:18 pm, "Derek P." <de...-88kYAtoe8eYTx9n+QjaLhg@public.gmane.org> wrote: > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Jun 4, 6:13 pm, "Derek P." <de...-88kYAtoe8eYTx9n+QjaLhg@public.gmane.org> wrote:> 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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Jun 4, 8:18 pm, "Derek P." <de...-88kYAtoe8eYTx9n+QjaLhg@public.gmane.org> wrote: > > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Jun 4, 6:13 pm, "Derek P." <de...-88kYAtoe8eYTx9n+QjaLhg@public.gmane.org> wrote:> 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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Jun 4, 8:18 pm, "Derek P." <de...-88kYAtoe8eYTx9n+QjaLhg@public.gmane.org> wrote: > > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Jun 4, 7:53 pm, Robert Walker <r0b3rt4...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > On Jun 4, 6:13 pm, "Derek P." <de...-88kYAtoe8eYTx9n+QjaLhg@public.gmane.org> wrote: > > > 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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > On Jun 4, 8:18 pm, "Derek P." <de...-88kYAtoe8eYTx9n+QjaLhg@public.gmane.org> wrote: > > > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---