diff --git a/lib/aasm/persistence/mongoid_persistence.rb b/lib/aasm/persistence/mongoid_persistence.rb index bb1004b9..75660784 100644 --- a/lib/aasm/persistence/mongoid_persistence.rb +++ b/lib/aasm/persistence/mongoid_persistence.rb @@ -99,8 +99,11 @@ def aasm_ensure_initial_state attribute_name = self.class.aasm(state_machine_name).attribute_name.to_s # Do not load initial state when object attributes are not loaded, # mongoid has_many relationship does not load child object attributes when - # only ids are loaded, for example parent.child_ids will not load child object. - if attribute_names.include?(attribute_name) && attributes[attribute_name].blank? && !attribute_missing?(attribute_name) + # only ids are loaded, for example parent.child_ids will not load child object attributes. + # This feature is introduced in mongoid > 4. + if attribute_names.include?(attribute_name) && attributes[attribute_name].blank? + # attribute_missing? is defined in mongoid > 4 + return if Mongoid::VERSION.to_f >= 4 && attribute_missing?(attribute_name) send("#{self.class.aasm(state_machine_name).attribute_name}=", aasm(state_machine_name).enter_initial_state.to_s) end end