1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

Fix Mongoid has_many association _ids helper issue

- fix #332
This commit is contained in:
Anil 2016-02-26 03:04:49 +05:30
parent 24b1ad99ec
commit 54c4fc4c5d
3 changed files with 44 additions and 1 deletions

View file

@ -96,7 +96,13 @@ module AASM
#
def aasm_ensure_initial_state
AASM::StateMachine[self.class].keys.each do |state_machine_name|
send("#{self.class.aasm(state_machine_name).attribute_name}=", aasm(state_machine_name).enter_initial_state.to_s) if send(self.class.aasm(state_machine_name).attribute_name).blank?
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)
send("#{self.class.aasm(state_machine_name).attribute_name}=", aasm(state_machine_name).enter_initial_state.to_s)
end
end
end
end # InstanceMethods

View file

@ -0,0 +1,26 @@
class Parent
include Mongoid::Document
include AASM
field :status, :type => String
has_many :childs
aasm column: :status do
state :unknown_scope
state :new
end
end
class Child
include Mongoid::Document
include AASM
field :parent_id
field :status, :type => String
belongs_to :parent
aasm column: :status do
state :unknown_scope
state :new
end
end

View file

@ -71,6 +71,17 @@ describe 'mongoid' do
end
describe "relations object" do
it "should load relations object ids" do
parent = Parent.create
child_1 = Child.create(parent_id: parent.id)
child_2 = Child.create(parent_id: parent.id)
expect(parent.child_ids).to eql [child_1.id, child_2.id]
end
end
rescue LoadError
puts "--------------------------------------------------------------------------"
puts "Not running Mongoid specs because mongoid gem is not installed!!!"