Question

In my current application, I need the ability to track points on a weekly basis so that the point totals for the user reset back to zero each week. I was planning on using the gem merit: https://github.com/tute/merit to track points.

In my users profile I have a field that is storing the points. What I have been unable to locate is how I can have rails on an auto basis for all users clear this field.

I have come across some information Rails reset single column I think this may be the answer in terms of resetting it every Sunday at a set time -- but I am uncertain on this last part and in addition where the code would go (model or controller)

Also, would welcome any suggestions if their is a better method.

Was it helpful?

Solution

You'd be better making a Point model, which belongs_to :user

This will allow you to add any points you want, and can then query the table based on the created_at column to get a .count of the points for the timespan you want

I can give you more info if you think it appropriate


Models

One principle we live by is to extend our models as much as possible

You want each model to hold only its data, thus ensuring more efficient db calls. I'm not super experienced with databases, but it's my opinion that having a lot of smaller models is more efficient than one huge model

So in your question, you wanted to assign some points to a user. The "right" way to do this is to store all the points perpetually; which can only be done with its own model


Points

#app/models/point.rb
Class Point < ActiveRecord::Base
    belongs_to :user
end

#app/models/user.rb
Class User < ActiveRecord::Base
    has_many :points
end

Points table could look like this:

points
id | user_id | value | created_at | updated_at

Saving

To save the points, you will literally just have to add extra records to the points table. The simplest way to achieve this will be to merge the params, like this:

#app/controllers/points_controller.rb
class PointsController < ApplicationController

    def new
        @points = Point.new
    end

    def create
        @points = Point.new(points_params)
        @points.save
    end

    private
    def points_params
        params.require(:points).permit(:value).merge(:user_id => current_user.id)
    end
end

You can define the "number" of points by setting in the value column (which I'd set as default 1). This will be how StackOverflow gives different numbers of points; by setting the value column differently ;)


Counting

To get weekly countings, you'll have to create some sort of function which will allow you to split the points by week. Like this:

#app/models/point.rb -> THIS NEEDS MORE WORK
def self.weekly
    where(:created_at => Time.now.next_week..Time.now.next_week.end_of_week)
end

That function won't work as it is I'll sort out the function properly for you if you let me know a little more about how you'd like to record / display the weekly stats. Is it going to be operated via a cron job or something?

OTHER TIPS

Based on your description, you might want to simply track the users points and the time that they got them. Then you can query for any 1 week period (or different periods if you decide you want all-time, annual, etc) and you won't lose historical data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top