2013-02-05 14:16:51 -05:00
|
|
|
class UserVisit < ActiveRecord::Base
|
|
|
|
attr_accessible :visited_at, :user_id
|
2013-02-27 22:39:42 -05:00
|
|
|
|
|
|
|
# A list of visits in the last month by day
|
2013-04-03 13:25:52 -04:00
|
|
|
def self.by_day(sinceDaysAgo=30)
|
|
|
|
where("visited_at >= ?", sinceDaysAgo.days.ago).group(:visited_at).order(:visited_at).count
|
2013-02-27 22:39:42 -05:00
|
|
|
end
|
2013-04-05 02:43:48 -04:00
|
|
|
|
|
|
|
def self.ensure_consistency!
|
|
|
|
exec_sql <<SQL
|
|
|
|
UPDATE users u set days_visited =
|
|
|
|
(
|
|
|
|
SELECT COUNT(*) FROM user_visits v WHERE v.user_id = u.id
|
|
|
|
)
|
|
|
|
WHERE days_visited <>
|
|
|
|
(
|
|
|
|
SELECT COUNT(*) FROM user_visits v WHERE v.user_id = u.id
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|