class WatchersOfProjects < ActiveRecord::Base attr_accessible :project_id, :user_id belongs_to :project belongs_to :user validate :validate_user validate :validate_project validates_uniqueness_of :user_id, :scope => :project_id validates_presence_of :user_id, :project_id def self.watch(user_id, project_id) @new_watch = WatchersOfProjects.new @new_watch.user_id = user_id @new_watch.project_id = project_id @new_watch.save true end def self.watcher_count(project) @project = project @count = @project.watchers_of_projects.count @count end def self.is_watched(user_id, project_id) @is_watched = self.where("user_id = ? and project_id = ?", user_id, project_id).to_a.first if @is_watched.nil? false else true end end def self.watch_cancle(user_id, project_id) self.delete_all(["user_id = ? and project_id = ?", user_id, project_id]) true end def validate_user errors.add :user_id, :invalid if user.nil? || !user.active? end def validate_project errors.add :project_id, :invalid if project.nil? end end