Yea you're right, it's a bug because doing the same thing with #all or
#first does not update the query. I believe the issue is in #last
because it calls Query#reverse! which permanently changes the query
but I haven't written any tests. I'll work on it and submit a patch.

On Mar 4, 8:15 am, MarkMT <mark.thom...@ieee.org> wrote:
> Rip, I'm still not persuaded that this is not a bug. Regarding this -
>
> > "s.last.name" the query is updated with #last, so now the query is
> > like this: User.all(:order => [:age.asc]).last => which is like
> > User.all(:order => [:age.asc]).update(:limit => 1, :order =>
> > [:age.desc])
>
> I agree with your description of what the query required to retrieve
> the last element looks like, but there's nothing in the API
> documentation to suggest that #last modifies its receiver in the
> process. Rather it says that it *returns* the "last resource in the
> entries of this collection, or a new collection whose query has been
> merged". This is contrast to the documentation for #update which
> states explicitly that it returns 'self'.
>
> I agree that #last does seem to be modifying the query associated with
> the collection it is called on, but I don't see any reason that this
> would be the intended behavior.
>
> On Mar 3, 7:56 pm, RipTheJacker <kab...@gmail.com> wrote:
>
>
>
> > It's calling an extra query and leaving it there:
>
> > "first query
> >  ~ (0.000097) SELECT "id", "age", "name" FROM "users" ORDER BY "age"
> > ["Jill", "Mary", "Bob", "Jack"]
> > "Jack"
> > ["Jill", "Mary", "Bob", "Jack"]
> > second query
> >  ~ (0.000081) SELECT "id", "age", "name" FROM "users" ORDER BY "age"
> > DESC LIMIT 1
> > "Jack"
> >  ~ (0.000078) SELECT "id", "age", "name" FROM "users" ORDER BY "age"
> > DESC
> > ["Bob", "Mary", "Jill", "Jack"]"
>
> > I don't think it is a bug, here's how it works:
>
> > "s = User.all(:order => [:age.asc])"
> > "s" is just a DataMapper::Query until you call something on it.
> > "s.map{|r| r.name}" <- now you've called something so the query
> > executes here.
> > "s" is now a reference to a DataMapper::Collection, because you called
> > the query
> > "s.last.name" -> #last is calling DataMapper::Collection#last
>
> > In the second part:
> > "s = User.all(:order => [:age.asc])"
> > "s" again is just a query at this point
> > "s.last.name" the query is updated with #last, so now the query is
> > like this: User.all(:order => [:age.asc]).last => which is like
> > User.all(:order => [:age.asc]).update(:limit => 1, :order =>
> > [:age.desc]) because DM will give you the "last result in users sorted
> > by age asc" which is the same as saying the "first result in users
> > sorted by age desc"
>
> > Here's how it -might- be a bug... (in the second example)
> > "s" is a reference to an updated query as opposed to a single User
> > instance, or it might be a collection, I don't know here...
> > "s.map{|r| r.name}" #map is being called on the updated query
>
> > On Feb 28, 11:01 pm, MarkMT <mark.thom...@ieee.org> wrote:
>
> > > Sorry, I pasted the url for another gist I had been looking at from a
> > > different thread. This is the one I meant to refer to -
>
> > >http://gist.github.com/318072
>
> > > On Feb 28, 10:57 pm, MarkMT <mark.thom...@ieee.org> wrote:
>
> > > > I've run into a problem that seems like it might be a bug...
>
> > > > If I retrieve a set of records using the ':order' option and then call
> > > > 'last' on the resulting collection, the collection seems to end up
> > > > being mis-ordered. The problem only seems to manifest itself if I
> > > > haven't made an attempt to do anything with the ordered collection
> > > > prior to calling 'last'.
>
> > > > Here's a gist that illustrates -http://gist.github.com/305019.
>
> > > > This is with dm 0.10.2. Am I misunderstanding how this should work?
>
> > > > Mark.

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to datamap...@googlegroups.com.
To unsubscribe from this group, send email to 
datamapper+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to