# Should you use scopes or class methods?

DevFeed: [Should you use scopes or class methods?](<https://devfeed.tech/articles/should-you-use-scopes-or-class-methods-26274.md>)

Original publisher: [Read original article](<https://www.justinweiss.com/articles/should-you-use-scopes-or-class-methods/>)

Author: Justin Weiss

Published: 2015-09-01T07:30:32Z

Content type: tutorial

Language: en

Sources: [Justin Weiss](<https://devfeed.tech/sources/justin-weiss.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [code](<https://devfeed.tech/tags/code.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [scopes](<https://devfeed.tech/tags/scopes.md>)

## AI overview

The article compares Rails scopes with regular Ruby class methods. It explains that scopes are useful for simple, chainable object selection and express intent, while class methods are preferable for more complicated logic or when preloading scopes is needed.

## Source excerpt

This article is also available in Korean, thanks to Soonsang Hong! Scopes are a great way to grab the right objects out of your database: app/models/review.rbclass 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.rb@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.rbdef self.most_recent(limit) order("created_at desc").limit(limit) end app/controllers/homepage_controller.rb@recent_reviews = Review.most_recent(5) So why would you use a scope when you could use regular Ruby class methods? Is it worth keeping these totally separate, but equivalent, concepts in your head? What if you run into weird bugs? Isn't all this extra stuff the kind of thing that makes Rails harder to learn? When would it make sense to use a scope instead of a class method? Why use scopes when we already have class methods? What if you wanted to grab all the reviews written after a specific date? But if no date was specified, you wanted all the reviews returned instead? As a scope, that looks like this: app/models/review.rbscope :created_since, ->(time) { where("reviews.created_at > ?", time) if time.present? } Easy enough, right? What about the class method? app/models/review.rbdef self.created_since(time) if time.present? where("reviews.created_at > ?", time) else all end end It takes a little bit of extra work. Scopes prefer to return scopes, so they're easy to chain together: Review.positive.created_since(5.days.ago) But to get the class method to work the same way, you have to specifically handle the case where time is nil. Otherwise, the caller would have to figure out whether it has a valid, chainable scope. Methods that always return the same kind of object are really useful. You don't have to worry as much about edge cases or errors