Question

Which one would be preferable to use? Named scope or class methods? The use case is that the user selects a list box in the UI and based on the selected item, the table beneath it gets sorted. This functionality is across the application and is for many models.

Was it helpful?

Solution

The most important thing is that you do what feels right and that you're consistent with it. If something seems like it would be better in a method, use a method.

As of Rails 3, named scopes and class methods can function almost identically. However, the only "rules" I tend to follow when deciding on one or the other:

  • Use named scopes when an argument is NOT required
  • Use class methods when an argument is required

I also tend to use named scopes when I plan on chaining them together, as they tend to be short with minimal logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top