2013-08-27 11:27:25 +08:00
|
|
|
class UserStatus < ActiveRecord::Base
|
|
|
|
attr_accessible :changesets_count, :user_id, :watchers_count
|
2014-07-10 15:26:34 +08:00
|
|
|
belongs_to :user
|
2013-08-27 11:27:25 +08:00
|
|
|
belongs_to :watchers
|
|
|
|
belongs_to :changesets
|
|
|
|
validates_presence_of :user_id
|
|
|
|
validates_uniqueness_of :user_id
|
|
|
|
|
2013-09-10 21:02:29 +08:00
|
|
|
scope :visible, lambda {|*args| nil }
|
2014-02-24 10:40:45 +08:00
|
|
|
scope :teacher, -> {
|
|
|
|
joins('LEFT JOIN users ON user_statuses.user_id=users.id').
|
|
|
|
joins('LEFT JOIN user_extensions ON users.id=user_extensions.user_id').
|
|
|
|
where('user_extensions.identity = ?', 0)
|
|
|
|
}
|
|
|
|
scope :student, -> {
|
|
|
|
joins('LEFT JOIN users ON user_statuses.user_id=users.id').
|
|
|
|
joins('LEFT JOIN user_extensions ON users.id=user_extensions.user_id').
|
|
|
|
where('user_extensions.identity = ?', 1)
|
|
|
|
}
|
|
|
|
|
2013-08-27 11:27:25 +08:00
|
|
|
# 更新字段 watchers_count 加1 这里没有做用户是否存在的匹配
|
|
|
|
# 负责这个表的聂同学 是在新建用户时就新建了该表的记录
|
|
|
|
# 但是 如果超级用户删除其他用户的话会造成读取错误 这里是遗漏点
|
|
|
|
# 删除用户时 此表创建人员未作相应删除动作
|
|
|
|
def update_watchers_count(num)
|
|
|
|
if self.watchers_count >= 0
|
|
|
|
self.update_attribute(:watchers_count, self.watchers_count.to_i + num)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|