Ruby on Rails:如何总结数据库中的这些元素?

时间:2020-12-11 07:11:12

I have a video_votes table with all the votes with a column called value set to 1 or -1. I want to sum up all the values of the video's votes and display that net vote count. First, how should I sum this up, and second, should I store this value in my video table? If so, how?

我有一个video_votes表,其中包含所有选票,列名为value set to 1或-1。我想把视频投票的所有值加起来然后显示净投票数。首先,我该如何总结,其次,我应该将这个值存储在我的视频表中吗?如果是这样,如何?

4 个解决方案

#1


7  

I would start with this until performance became an issue:

我将从这个开始,直到性能成为一个问题:

class Video < AR::Base
  has_many :video_votes

  def vote_sum
    video_votes.sum(:value)
  end
end

class VideoVote < AR::Base
  belongs_to :video
  validates_inclusion_of :value, :in => [-1,1]
end

Once performance became an issue and I wanted to cache the summed value I might do something like this:

一旦性能成为一个问题,我想要缓存的求和值,我可能会这样做:

class Video < AR::Base
  has_many :video_votes

  # Override vote_sum attribute to get the db count if not stored in the db yet. 
  # The alternative is that you could remove this method and have the field
  # populated by a migration.
  def vote_sum
    read_attribute(:vote_sum) || video_votes.sum(:value)
  end
end

class VideoVote < AR::Base
  belongs_to :video
  validates_inclusion_of :value, :in => [-1,1]

  after_create :update_video_vote_sum

private

  def update_video_vote_sum
    video.update_attributes(:vote_sum => video.vote_sum + value)
  end
end

Check out the AR documentation on "Overwriting default accessors" (scroll down a bit) http://ar.rubyonrails.org/classes/ActiveRecord/Base.html

查看“覆盖默认访问器”的AR文档(向下滚动一点)http://ar.rubyonrails.org/classes/ActiveRecord/Base.html

#2


1  

In your Video model:

在你的视频模式:

def total_votes
  self.votes.sum(:value)
end

So an example might be:

举个例子:

@video.total_votes

#3


0  

Use ActiveRecord's sum method.

使用ActiveRecord的求和方法。

VideoVote.sum('value')

You shouldn't store it in the same table. If you have other fields you want to summarize then create a "summary" table and periodically summarize the fields and store the values there. ActiveRecord's other calculation methods might be of interest in that case.

您不应该将它存储在同一个表中。如果您有其他需要总结的字段,那么创建一个“summary”表,并定期总结字段并在其中存储值。ActiveRecord的其他计算方法可能对这种情况感兴趣。

#4


0  

I'm to sum 2 fields on the level a model:

我将对a级模型的两个字段进行求和:

  def update_sum_times
    update_attribute(:sum_of_fields, calc_two_dates)
  end

  def calc_two_dates
    date_one + date_two
  end

#1


7  

I would start with this until performance became an issue:

我将从这个开始,直到性能成为一个问题:

class Video < AR::Base
  has_many :video_votes

  def vote_sum
    video_votes.sum(:value)
  end
end

class VideoVote < AR::Base
  belongs_to :video
  validates_inclusion_of :value, :in => [-1,1]
end

Once performance became an issue and I wanted to cache the summed value I might do something like this:

一旦性能成为一个问题,我想要缓存的求和值,我可能会这样做:

class Video < AR::Base
  has_many :video_votes

  # Override vote_sum attribute to get the db count if not stored in the db yet. 
  # The alternative is that you could remove this method and have the field
  # populated by a migration.
  def vote_sum
    read_attribute(:vote_sum) || video_votes.sum(:value)
  end
end

class VideoVote < AR::Base
  belongs_to :video
  validates_inclusion_of :value, :in => [-1,1]

  after_create :update_video_vote_sum

private

  def update_video_vote_sum
    video.update_attributes(:vote_sum => video.vote_sum + value)
  end
end

Check out the AR documentation on "Overwriting default accessors" (scroll down a bit) http://ar.rubyonrails.org/classes/ActiveRecord/Base.html

查看“覆盖默认访问器”的AR文档(向下滚动一点)http://ar.rubyonrails.org/classes/ActiveRecord/Base.html

#2


1  

In your Video model:

在你的视频模式:

def total_votes
  self.votes.sum(:value)
end

So an example might be:

举个例子:

@video.total_votes

#3


0  

Use ActiveRecord's sum method.

使用ActiveRecord的求和方法。

VideoVote.sum('value')

You shouldn't store it in the same table. If you have other fields you want to summarize then create a "summary" table and periodically summarize the fields and store the values there. ActiveRecord's other calculation methods might be of interest in that case.

您不应该将它存储在同一个表中。如果您有其他需要总结的字段,那么创建一个“summary”表,并定期总结字段并在其中存储值。ActiveRecord的其他计算方法可能对这种情况感兴趣。

#4


0  

I'm to sum 2 fields on the level a model:

我将对a级模型的两个字段进行求和:

  def update_sum_times
    update_attribute(:sum_of_fields, calc_two_dates)
  end

  def calc_two_dates
    date_one + date_two
  end