socialforge/app/models/user_status.rb

32 lines
1.2 KiB
Ruby
Raw Permalink Normal View History

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 }
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)
}
# 更新字段 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