0

How can I grab a User's points for a single day (i.e. 'Today' or 'Yesterday') using the Merit gem?

I tried:

current_user.points.where("created_at >= ?", Time.zone.now.beginning_of_day)

but that doesn't work.

John Trichereau
  • 626
  • 9
  • 22
  • I think you should look at the source of the gem. For example [this is method `points`](https://github.com/tute/merit/blob/master/lib/merit/models/active_record/merit/score.rb#L47) and it returns number! But you can see that number is a result of query to `score_points`. I think you have a chance to fetch points for a day if you manage to add a scope to the table `score_points`. Maybe you can override the whole method `points`. – gotva Jan 13 '14 at 14:41

2 Answers2

1

There's a models diagram for merit in https://github.com/tute/merit/wiki/General-merit-workflow. With that in mind, lines like this makes it work:

user = User.first
points = user.sash.scores.first.score_points
points.where("created_at > '#{Time.zone.now.beginning_of_day}'")
TuteC
  • 4,342
  • 30
  • 40
  • I got it working with the full line as follows: u.sash.scores.first.score_points.where("created_at > ?", Time.zone.now.beginning_of_day).sum(:num_points) – John Trichereau Jan 13 '14 at 17:27
0

This is the final answer:

u.sash.scores.first.score_points.where("created_at > ?", Time.zone.now.beginning_of_day).sum(:num_points)

Thanks TuteC.

John Trichereau
  • 626
  • 9
  • 22