Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4弃用警告:关系#calculate with finder选项已弃用。请构建一个作用域,然后对其调用calculate_Ruby On Rails_Deprecation Warning - Fatal编程技术网

Ruby on rails Rails 4弃用警告:关系#calculate with finder选项已弃用。请构建一个作用域,然后对其调用calculate

Ruby on rails Rails 4弃用警告:关系#calculate with finder选项已弃用。请构建一个作用域,然后对其调用calculate,ruby-on-rails,deprecation-warning,Ruby On Rails,Deprecation Warning,以下是“酒店”模型中查找交易和用户方面最高消费者的方法 def top_spenders top_spenders = [] self.transactions.group(:user_id).sum(:amount, :order => 'SUM(amount) DESC', :limit => 5).each do |record| person = User.find(record.first).name amount = re

以下是“酒店”模型中查找交易和用户方面最高消费者的方法

def top_spenders
    top_spenders = []
    self.transactions.group(:user_id).sum(:amount, :order => 'SUM(amount) DESC', :limit => 5).each do |record|
        person = User.find(record.first).name 
        amount = record.second
        person_amount = [person, amount]
        top_spenders << person_amount
    end 
    top_spenders
end

### hotel.rb
    has_many :transactions

### transaction.rb
    belongs_to :user
    belongs_to :hotel
弃用警告如下所示:

DEPRECATION WARNING: Relation#calculate with finder options is deprecated. Please build a scope and then call calculate on it instead. (called from top_spenders ...
DEPRECATION WARNING: The :distinct option for `Relation#count` is deprecated. Please use `Relation#distinct` instead. (eg. `relation.distinct.count`). (called from top_spenders ...
我确实尝试为分组事务添加作用域,然后在其中查询sum,但警告仍然存在。此外,还尝试了其他一些方法,但在满足需求的情况下无法避免遭到反对

任何帮助都会受到极大的赞赏

self.transactions.group("user_id").select("user_id, sum(amount) as total_amount").order("sum(amount) DESC").includes(:user)
这修复了弃用警告并启用了即时加载。多亏了Rails 4.:)

self.transactions.group("user_id").select("user_id, sum(amount) as total_amount").order("sum(amount) DESC").includes(:user)