You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/app/models/user_status.rb

32 lines
1.2 KiB

6 years ago
class UserStatus < ActiveRecord::Base
attr_accessible :changesets_count, :user_id, :watchers_count
belongs_to :user
belongs_to :watchers
belongs_to :changesets
validates_presence_of :user_id
validates_uniqueness_of :user_id
scope :visible, lambda {|*args| nil }
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)
}
# 更新字段 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