Grouping by week/month/etc & ActiveRecord?

前端 未结 7 1286
野的像风
野的像风 2020-12-01 09:22

I\'m doing some statics calculation in my product. A user has performed a number of operations, let\'s say posted comments. I want to be able to show them how many comment

相关标签:
7条回答
  • 2020-12-01 09:41

    Use group_by

    @user.comments.group_by(&:week)
    
    class User < ActiveRecord::Base
      def week
        some_attribute_like_date.strftime('%Y-%W')
      end
    end
    

    This will give you a grouped list in the format of YYYY-WW

    0 讨论(0)
提交回复
热议问题