class ApplicationFormSignupContribute include Mongoid::Document include Mongoid::Timestamps field :title field :abstract_number , type: String , default: "" field :presentation_type , type: String , default: "" field :sort_number , type: Integer, default: 10000 mount_uploader :file, AssetUploader field :description field :preferred_session field :final_session belongs_to :application_form_signup has_one :application_form_review_result, dependent: :destroy accepts_nested_attributes_for :application_form_review_result has_many :application_form_submission_values, autosave: true, dependent: :destroy accepts_nested_attributes_for :application_form_submission_values, :allow_destroy => true scope :sort_ordered, ->{ order_by(:sort_number=>1,:created_at=>1) } after_destroy do application_form_signup = self.application_form_signup.reload rescue nil application_form_signup_id = application_form_signup.id.to_s application_form_submission_field_ids = application_form_signup.application_form_main.application_form_submission_fields.where(:markup=>"application_form_preferred_session").pluck(:id) rescue [] if application_form_signup && application_form_submission_field_ids.count != 0 application_form_signup_contribute_ids = application_form_signup.application_form_signup_contributes.pluck(:id) application_form_submission_values = ApplicationFormSubmissionValue.where(:application_form_signup_contribute_id.in=>application_form_signup_contribute_ids,:application_form_submission_field_id.in=>application_form_submission_field_ids).to_a application_form_sessions = application_form_signup.application_form_main.application_form_sessions.to_a application_form_sessions.each do |ss| ss.default_application_form_signup_ids.delete(application_form_signup_id) ss.save(:validate=>false) end application_form_submission_value = application_form_submission_values.last val = application_form_submission_value["val"].to_i rescue nil unless val.nil? application_form_session = application_form_sessions.select{|ss| ss.key == val}.first if application_form_session application_form_session.default_application_form_signup_ids << application_form_signup_id application_form_session.save(:validate=>false) end end end end end