How to rewrite rails group_by class method as scope

情到浓时终转凉″ 提交于 2019-12-11 13:49:11

问题


I would like to rewrite my class method as a scope.

class Team

 def self.grouped
   self.all.group_by { |e| e.type }.map { |k, v| { k => v.group_by { |e| e.sub_type } } }
 end

end

How would I write as a scope?

class Team

 #  scope :grouped ??

end

回答1:


You cannot write this as a scope. Scopes in Rails act on ActiveRecord::Relation objects and are supposed to generate SQL queries that run against the database.

But the group_by method is called on the array after the data was received from the database.

You will always have to load the data from the database first, before you can group it with group_by.

You could write your own nested_group_by method on Array:

class Array
  def nested_grouped_by(group_1, group_2)
    group_by { |e| e.send(group_1) }.
      map { |k, v| { k => v.group_by { |e| e.send(group_2) } } }
  end
end

That could be used like this:

Team.all.nested_grouped_by(:type, :subtype)

Note the all that force the scope to actually load the data from the database and returns an array.



来源:https://stackoverflow.com/questions/36390144/how-to-rewrite-rails-group-by-class-method-as-scope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!