class ArchiveSortOrder include Mongoid::Document field :sort_order, :type => Boolean # true => desc , false => asc after_initialize do |record| if(!record.new_record? && record.sort_order.class != FalseClass && record.sort_order.class != TrueClass) record.sort_order = true record.save elsif(!record.new_record?) @module_app = ModuleApp.where(:key=>'archive').first if !@module_app.nil? @asc = @module_app.asc rescue nil if !@asc.nil? record.sort_order = @asc record.save end end end end after_save do |record| @module_app = ModuleApp.where(:key=>'archive').first if !@module_app.nil? @sort_order = record.sort_order rescue nil if !@asc.nil? if !@module_app.attributes[:asc].nil? @module_app.asc = @sort_order @module_app.save end end end end end