Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Relation#last has issues with existing order_clauses #1272

Closed
ericallam opened this Issue · 4 comments

4 participants

@ericallam

On rc1, if a model has a default_scope with an order clause, #last does not generate the correct sql query, resulting in the same object as #first being returned.

For example, lets say I have a User model with a default_scope with created_at ASC:

class User < ActiveRecord::Base
  def self.default_scope
     order('created_at ASC')
  end
end

Then User.last generates this sql:

SELECT "users".* FROM "users" ORDER BY created_at ASC, created_at DESC LIMIT 1

So:

>> User.first == User.last
  User Load (3.1ms)  SELECT "users".* FROM "users" ORDER BY created_at ASC LIMIT 1
  User Load (1.1ms)  SELECT "users".* FROM "users" ORDER BY created_at ASC, created_at DESC LIMIT 1
=> true

It looks like it only happens with order clauses in the default_scope. (also effects #last in a relationship call e.g. project.users.last)

@pixeltrix
Owner

This is to do with how the default scope is merged in last now in 3.1 - AR::Relation#last calls reverse_order internally which it does on the order in the default scope but arel merges in the default scope again so you end up with two order clauses.

@jonleighton would we be better off merging in the default scope in the finder methods like find_first, find_last, etc. rather than merging in the arel method? Using with_default_scope.reverse_order in AR::Relation#last seems to fix this.

@jonleighton
Collaborator

@pixeltrix We need to change reverse_order to just set a reverse_order_value flag, and then deal with it properly in build_arel... I had to do the same thing for reorder when I originally made the default scope change, but I missed this one.

I will fix when I get a chance - or feel free to take it in the mean time :)

@jonleighton jonleighton was assigned
@winterchord winterchord referenced this issue from a commit
@winterchord winterchord Fix issue #1272
Set reverse_order_value when asked to reverse_order().
Do the actual reversal in build_arel.
9d65604
@winterchord winterchord referenced this issue from a commit
@winterchord winterchord Amendment to #1272
Allow reverse_order.reverse_order to produce the original order.
1a2be42
@winterchord winterchord referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@winterchord winterchord referenced this issue from a commit in winterchord/rails
@winterchord winterchord Fix issue #1272
Set reverse_order_value when asked to reverse_order().
Do the actual reversal in build_arel.
c8d717a
@winterchord winterchord referenced this issue from a commit in winterchord/rails
@winterchord winterchord Fix issue #1272
Set reverse_order_value when asked to reverse_order().
Do the actual reversal in build_arel.
23d012e
@jonleighton jonleighton referenced this issue from a commit
@winterchord winterchord Fix issue #1272
Set reverse_order_value when asked to reverse_order().
Do the actual reversal in build_arel.
8a4013d
@jonleighton jonleighton referenced this issue from a commit
@winterchord winterchord Fix issue #1272
Set reverse_order_value when asked to reverse_order().
Do the actual reversal in build_arel.
1e43bd9
@jonleighton jonleighton closed this
@oriolgual

This has introduced a new problem:

>> Fuu.order(Fuu.arel_table[:name].desc).reverse_order.to_sql
=> "SELECT \"fuus\".* FROM \"fuus\"  ORDER BY #<Arel::Nodes::Ordering:0x0000010319dc88> DESC"

Versus:

>> Fuu.order(Fuu.arel_table[:name].desc.to_sql).reverse_order.to_sql
=> "SELECT \"fuus\".* FROM \"fuus\"  ORDER BY \"fuus\".\"name\" ASC"

Notice the to_sql call in Fuu.arel_table[:name].desc

@jonleighton
Collaborator

I have filed a new issue for this: #1571

@jake3030 jake3030 referenced this issue from a commit in jake3030/rails
@bschwartz bschwartz Ruby 1.9 compat: fix for SSL in Active Resource
[#1272 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
2013874
@ttosch ttosch referenced this issue from a commit
@winterchord winterchord Fix issue #1272
Set reverse_order_value when asked to reverse_order().
Do the actual reversal in build_arel.
d3dcf17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.