Nick Ginanto
Nick Ginanto

Reputation: 32120

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?

Upvotes: 0

Views: 124

Answers (1)

x1a4
x1a4

Reputation: 19475

If you don't need to query for the dates directly, take a look at 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.

Another solution might be to use a Redis sorted set, using the date timestamp as the score, and the new followers as the value.

Upvotes: 1

Related Questions