Skip to content
This repository

default_scope doesn't let override the order #904

Closed
lighthouse-import opened this Issue · 1 comment

2 participants

Lighthouse Import Bryan Deter
Lighthouse Import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/6532
Created by Arik Fraimovich - 2011-03-05 08:32:24 UTC

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.

Lighthouse Import lighthouse-import closed this
Bryan Deter

Note that while fixed, you need to use the arel syntax for the scope (default_scope order('created_at DESC'))

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.