Created
August 3, 2010 09:11
-
-
Save larskuhnt/506078 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Comment < ActiveRecord::Base | |
belongs_to :commentable, :polymorphic => true | |
default_scope :order => 'created_at ASC' | |
named_scope :recent, lambda { |limit| { :order => 'created_at DESC', :limit => limit } } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Comment.recent(3) | |
# works as expected | |
# SELECT * FROM `comments` ORDER BY created_at DESC LIMIT 3 | |
Group.first.comments.recent(3) | |
# wrong ascending ordering | |
# SELECT * FROM `comments` WHERE (`comments`.commentable_id = 1 AND `comments`.commentable_type = 'Group') ORDER BY created_at ASC LIMIT 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Group < ActiveRecord::Base | |
has_many :comments, :as => :commentable | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment