Scopes are a great way to grab the right objects out of your database:
app/models/review.rb1 2 3 class Review < ActiveRecord::Base scope :most_recent, - (limit) { order("created_at desc").limit(limit) } end
You’d use the scope like this:
app/models/homepage_controller.rb1 @recent_reviews = Review.most_recent(5)
Calling that scope, though, looks exactly like calling a class method on Review. And it’s easy to build it as a class method, instead:
app/models/review.rb1 2 3 def self.most_recent(...<
Published on September 01, 2015 00:30