I
I
IvanN7772016-10-01 19:07:11
Ruby on Rails
IvanN777, 2016-10-01 19:07:11

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

By the way, what does sorting by lft mean?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
CapeRatel, 2016-10-02
@IvanN777

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).

Николай Турнавиотов, 2016-10-03
@foxmuldercp

Менять дефолтный скоуп - плохая идея, кстати

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question