2013-02-06 03:16:51 +08:00
|
|
|
class UserVisit < ActiveRecord::Base
|
|
|
|
attr_accessible :visited_at, :user_id
|
2013-02-28 11:39:42 +08:00
|
|
|
|
|
|
|
# A list of visits in the last month by day
|
2013-04-04 01:25:52 +08:00
|
|
|
def self.by_day(sinceDaysAgo=30)
|
|
|
|
where("visited_at >= ?", sinceDaysAgo.days.ago).group(:visited_at).order(:visited_at).count
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
2013-04-05 14:43:48 +08: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-06 03:16:51 +08:00
|
|
|
end
|
2013-05-24 10:48:32 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_visits
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# visited_at :date not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_user_visits_on_user_id_and_visited_at (user_id,visited_at) UNIQUE
|
|
|
|
#
|
|
|
|
|