Answer the question
In order to leave comments, you need to log in
What is the correct way to organize sorting in Rails?
I organized the default sorting chain and allocated a scope for rating, since the rating can be redirected to a separate entity in the future, then I allocated it to a class method. Can I separate sorting into some kind of module, and from the point of view of a competent approach, did I do it right?
default_scope { sorting_rules_default }
scope :rating, -> { order_by_rating }
def self.sorting_rules_default
sorting_by_rating.order(name: :desc)
end
def self.sorting_by_rating
order(rating: :desc)
end
Answer the question
In order to leave comments, you need to log in
Do not fence anything in advance, do not optimize anything in advance.
Just write scopes and move on.
When you need to move tens/hundreds of scopes into a separate logic, you can use the repository pattern (google for help, you can also look at hanamirb.org , it is implemented here).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question