Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Relation#last has issues with existing order_clauses #1272

Closed
ericallam opened this Issue May 24, 2011 · 4 comments

Comments

Projects
None yet
4 participants
Contributor

ericallam commented May 24, 2011

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)

Owner

pixeltrix commented May 24, 2011

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.

Member

jonleighton commented May 24, 2011

@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 :)

@ghost ghost assigned jonleighton May 24, 2011

@jonleighton jonleighton added a commit that referenced this issue Jun 1, 2011

@winterchord @jonleighton winterchord + jonleighton Fix issue #1272
Set reverse_order_value when asked to reverse_order().
Do the actual reversal in build_arel.
8a4013d

@jonleighton jonleighton added a commit that referenced this issue Jun 1, 2011

@winterchord @jonleighton winterchord + jonleighton Fix issue #1272
Set reverse_order_value when asked to reverse_order().
Do the actual reversal in build_arel.
1e43bd9

@jonleighton jonleighton closed this Jun 1, 2011

Contributor

oriolgual commented Jun 8, 2011

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

Member

jonleighton commented Jun 8, 2011

I have filed a new issue for this: #1571

@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011

@bschwartz @jeremy bschwartz + jeremy Ruby 1.9 compat: fix for SSL in Active Resource
[#1272 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
2013874
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment