Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.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 SUM()不可变事务是可缓存的吗?_Ruby On Rails_Postgresql - Fatal编程技术网

Ruby on rails SUM()不可变事务是可缓存的吗?

Ruby on rails SUM()不可变事务是可缓存的吗?,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,环境: # models/user.rb # Table: users class User < ActiveRecord::Base has_many :points_transactions def points points_transactions.sum(:amount) end end # models/points_transaction.rb # Table: points_transactions # [Number] amount the p

环境:

# models/user.rb

# Table: users
class User < ActiveRecord::Base
  has_many :points_transactions

  def points
    points_transactions.sum(:amount)
  end
end

# models/points_transaction.rb

# Table: points_transactions
# [Number] amount the points of the transaction (could be negative)
# [Int] user_id the user it is connected to
class PointsTransaction < ActiveRecord::Base
  belongs_to :user
end
我有一个
用户
-模型,每个
用户都有很多:积分交易
点交易
有一个带有
:金额的字段
。如果我想知道用户的(积分)余额,我必须
求和(“积分交易”,“金额”)
。随着时间的推移,交易量开始增长(每分钟增加一次),而
总和将花费越来越多的时间(请参见下面的简化模型代码)

问题:

# models/user.rb

# Table: users
class User < ActiveRecord::Base
  has_many :points_transactions

  def points
    points_transactions.sum(:amount)
  end
end

# models/points_transaction.rb

# Table: points_transactions
# [Number] amount the points of the transaction (could be negative)
# [Int] user_id the user it is connected to
class PointsTransaction < ActiveRecord::Base
  belongs_to :user
end
我想知道的是,既然交易更像是发生了什么的历史,过去永远不会改变,那么有没有可能
SUM()
对交易进行缓存,以某种方式缓存它,这样当新交易出现时,您就可以使用缓存+新交易金额

清晰示例:

# models/user.rb

# Table: users
class User < ActiveRecord::Base
  has_many :points_transactions

  def points
    points_transactions.sum(:amount)
  end
end

# models/points_transaction.rb

# Table: points_transactions
# [Number] amount the points of the transaction (could be negative)
# [Int] user_id the user it is connected to
class PointsTransaction < ActiveRecord::Base
  belongs_to :user
end
用户Bob(id:1)在交易中获得5分:

  • 金额:+5
  • 金额:+10
  • 金额:-5
  • 金额:+15
  • 金额:-10
  • Bob想知道他的当前余额,所以他使用
    User.find(1).points\u transactions.sum(:amount)

    返回时间:15

    一个新的事务进入:+5。由于
    积分\u交易
    从未更改,因此不可能只获取上一个
    总和()的结果并向其添加+5吗?比如:

    15(以前的总和结果)+5(新交易)=20

    简化模型:

    # models/user.rb
    
    # Table: users
    class User < ActiveRecord::Base
      has_many :points_transactions
    
      def points
        points_transactions.sum(:amount)
      end
    end
    
    # models/points_transaction.rb
    
    # Table: points_transactions
    # [Number] amount the points of the transaction (could be negative)
    # [Int] user_id the user it is connected to
    class PointsTransaction < ActiveRecord::Base
      belongs_to :user
    end
    
    #models/user.rb
    #表:用户
    类用户

    我听说这在数据库级别上是可能的,但我找不到一种方法来真正做到这一点。你们会在数据库或Rails级别解决这个问题吗?让我知道

    嘿,但是基于调用user.points时的model方法,它将始终向您发送总和。“我也很困惑,你到底在要求什么呢?”巴拉松尼说,现在是这样,直到我找到一个更好的解决办法。