Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

default_scope doesn't let override the order

#6532

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.

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

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Arik Fraimovich
    Arik Fraimovich
    • No changes were found…

    March 5th, 2011 @ 08:44 AM