class RulingTimerTask include Mongoid::Document include Mongoid::Timestamps field :task_name, type: String, default: "" has_many :ruling_timer_sub_tasks, :autosave => true, :dependent => :destroy field :user_ids, type: Array, default: [] field :is_finished, type: Boolean, default: false def users User.where(:id.in=>self.user_ids) end after_save do |record| if record.user_ids_changed? delete_user_ids = record.user_ids_was.to_a - record.user_ids.to_a add_user_ids = record.user_ids.to_a - record.user_ids_was.to_a delete_user_ids.each do |user| self.ruling_timer_sub_tasks.where(:user=>user).destroy end add_user_ids.each do |user| self.ruling_timer_sub_tasks.create(:user=>user,:task_name=>task_name) end end if record.task_name_changed? self.ruling_timer_sub_tasks.each do |sub_task| sub_task.update(:task_name=>task_name) end end end end