Skip to content

Instantly share code, notes, and snippets.

@larskuhnt
Created August 3, 2010 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larskuhnt/506078 to your computer and use it in GitHub Desktop.
Save larskuhnt/506078 to your computer and use it in GitHub Desktop.
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 } }
}
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
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