简体   繁体   English

在Rails中保存模型的每日数据

[英]Saving daily data for a model in Rails

I have an article model and a user model. 我有一个文章模型和一个用户模型。 The user can follow that article, but can't unfollow it. 用户可以关注该文章,但不能取消关注。 I'd like to keep track of new followers for an article, and to retrieve it for when I want to plot it. 我想跟踪文章的新关注者,并在需要绘制文章时对其进行检索。 (Assume, for now, that plotting is not the problem.) (现在就假定绘图不是问题。)

I'd like every article to have a list of the every date since that article was created. 我希望每一篇文章都有自创建该文章以来的每个日期的列表。 So it will have: 因此它将具有:

article.newfollowers[1/1/2012] = 35
article.newfollowers[2/1/2012] = 4

Every time a user follows an article I would do 每次用户关注文章时,我都会做

This.newfollowers[Date()]++

Obviously saving such data means that each row in the article database has hundred of attributes (for each date). 显然,保存此类数据意味着商品数据库中的每一行都有数百个属性(对于每个日期)。

How would I save the data within the article model? 如何在商品模型中保存数据? How do I declare/define such attribute? 如何声明/定义此类属性?

If you don't need to query for the dates directly, take a look at ActiveRecord::Store . 如果您不需要直接查询日期,请查看ActiveRecord :: Store You'll need to be using Rails 3.2.0 at least, but would prevent you from having to add so many columns. 您至少需要使用Rails 3.2.0,但是这将避免您必须添加太多列。

Another solution might be to use a Redis sorted set , using the date timestamp as the score, and the new followers as the value. 另一种解决方案可能是使用Redis排序集 ,将日期时间戳记作为得分,并将新的关注者用作值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM