This project is archived and is in readonly mode.

#6532 new
Arik Fraimovich

default_scope doesn't let override the order

Reported by Arik Fraimovich | March 5th, 2011 @ 08:32 AM

I have the following code:

class Post < ActiveRecord::Base
  default_scope :order => 'created_at DESC'

  has_many :replies, :class_name => 'Post', :foreign_key=>"parent_id", :order=>"created_at ASC"
end

In Rails 2.3.8 when I was retrieving the replies (i.e. post.replies) I got them in the correct order. But in Rails 3.0.4 I get them in descending order, as if the order clause in the has_many association has no effect. When I checked the SQL resulting from this call I saw that it was generating the following:

SELECT `posts`.* FROM `posts` WHERE (`posts`.parent_id = 514) ORDER BY created_at desc, created_at ASC

While this behavior makes sense when the order is using two different fields, in case it is the same field it should just override the one in default scope.

This behavior is somewhat similar to ticket #2346, but there it was referencing Rails 2.3 and I wasn't it is exactly the same.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages