Scopes
class Comment < ActiveRecord::Base
scope :most_recent, -> (limit) { order("created_at desc").limit(limit) }
end
using scope
@recent_comments = Comment.most_recent(5)
Class Methods
In Model
def self.most_recent(limit)
order("created_at desc").limit(limit)
end
In controller
@recent_comments = Comment.most_recent(5)
Why would you use a scope when you could use regular Ruby class methods?