Skip to content

Commit

Permalink
Fix has_many association _ids helper for Mongoid 3
Browse files Browse the repository at this point in the history
  • Loading branch information
anilmaurya committed Feb 26, 2016
1 parent 54c4fc4 commit ec9a629
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/aasm/persistence/mongoid_persistence.rb
Expand Up @@ -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
Expand Down

0 comments on commit ec9a629

Please sign in to comment.