class SurveyAnswer include Mongoid::Document include Mongoid::Timestamps include Mongoid::Attributes::Dynamic field :user, type: BSON::ObjectId field :scored_points, type: Integer, :default => 0 field :individual_total, type: Array, :default => [] field :select_question, type: Array, :default => [] belongs_to :questionnaire_survey after_create do if self.questionnaire_survey answer_group = SurveyAnswerGroup.where(:questionnaire_survey_id=>self.questionnaire_survey.id,:user=>user).first if answer_group.nil? answer_group = SurveyAnswerGroup.new(:questionnaire_survey_id=>self.questionnaire_survey.id,:user=>user_id) end answer_group.last_modified = self.updated_at answer_group.survey_answer_ids << self.id answer_group.save end end after_save do answer_group = SurveyAnswerGroup.where(:questionnaire_survey_id=>self.questionnaire_survey.id,:user=>user).first if answer_group answer_group.last_modified = self.updated_at answer_group.save end end before_destroy do answer_group = SurveyAnswerGroup.where(:user=>user).first if answer_group answer_group.survey_answer_ids.delete(self.id) if answer_group.survey_answer_ids.count == 0 answer_group.destroy else answer_group.save end end end end