Ruby on rails 3 如何在使用Postgres的rails模型中使用.group和.sum?

Ruby on rails 3 如何在使用Postgres的rails模型中使用.group和.sum?,ruby-on-rails-3,postgresql,rails-postgresql,Ruby On Rails 3,Postgresql,Rails Postgresql,我在我的开发环境(SQLite)中有一个功能方法,它看起来是这样的: def self.ytd Production.find( :all, conditions: ["year == ?", Time.now.year], select: "carrier_id, amount, SUM(amount) as sum_amount", group: :carrier_id ) end 此方法成功搜索由:carrier\u id和:amount列组成的表

我在我的开发环境(SQLite)中有一个功能方法,它看起来是这样的:

def self.ytd
Production.find(
    :all,
    conditions: ["year == ?", Time.now.year],
    select: "carrier_id, amount, SUM(amount) as sum_amount",
    group: :carrier_id
    )
end
此方法成功搜索由
:carrier\u id
:amount
列组成的表。然后根据分组的
:carrier\u id
:amount
列求和

我正在尝试使用Postgresql转换为生产环境。下面的代码成功地对记录进行了分组,但我无法对
:金额进行求和。我尝试将
.sum(“productions.amount”)
修改为该方法,但没有成功。我相信有一个简单的解决办法,但它暗指我。。有人能帮忙吗?谢谢

def self.ytd
Production.select("DISTINCT ON (productions.carrier_id) productions.carrier_id, productions.amount ")
               .where("productions.year = (?)", "#{Time.now.year}")
               .group("productions.amount, productions.carrier_id, productions.id")
end

上删除了,将总和(金额)添加为总和,并在上分组:承运人id产生了所需的结果

def self.ytd
Production.select("productions.carrier_id, SUM(amount) as sum_amount")
               .where("productions.year = (?)", "#{Time.now.year}")
               .group("productions.carrier_id")      
end