Elegant PostgreSQL Group by for Ruby on Rails / ActiveRecord

前端 未结 3 891
南旧
南旧 2020-12-31 11:49

Trying to retrieve an array of ActiveRecord Objects grouped by date with PostgreSQL.

More specifically I\'m trying to translate the following MySQL query:

         


        
3条回答
  •  遥遥无期
    2020-12-31 12:25

    My solution:

    def self.columns_list
       column_names.collect { |c| "#{table_name}.#{c}" }.join(",")
     end
    
     scope :selling, joins(:products).group(columns_list)
    

    Simple and repeatable.

提交回复
热议问题