2013-02-05 14:16:51 -05:00
|
|
|
class UserVisit < ActiveRecord::Base
|
2013-02-27 22:39:42 -05:00
|
|
|
|
2014-01-24 15:19:20 -05:00
|
|
|
# A count 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
|
2013-10-03 23:28:49 -04:00
|
|
|
UPDATE user_stats u set days_visited =
|
2013-04-05 02:43:48 -04:00
|
|
|
(
|
2013-10-03 23:28:49 -04:00
|
|
|
SELECT COUNT(*) FROM user_visits v WHERE v.user_id = u.user_id
|
2013-04-05 02:43:48 -04:00
|
|
|
)
|
|
|
|
WHERE days_visited <>
|
|
|
|
(
|
2013-10-03 23:28:49 -04:00
|
|
|
SELECT COUNT(*) FROM user_visits v WHERE v.user_id = u.user_id
|
2013-04-05 02:43:48 -04:00
|
|
|
)
|
|
|
|
SQL
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_visits
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# visited_at :date not null
|
2014-02-06 19:07:36 -05:00
|
|
|
# posts_read :integer default(0)
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_user_visits_on_user_id_and_visited_at (user_id,visited_at) UNIQUE
|
|
|
|
#
|