-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
None
Opened on SO
I'm having a weird behavior with mongoid 3.0.22 and rails 3.1.10
Here are my classes:
class A
include Mongoid::Document
attr_accessible :b_attributes
def set_b!
self.b_attributes = [
]
end
end
class A1 < A
has_many :b, class_name: "B1", inverse_of: :a, autosave: true
accepts_nested_attributes_for :b, autosave: true
end
class A2 < A
has_many :b, class_name: "B2", inverse_of: :a, autosave: true
accepts_nested_attributes_for :b, autosave: true
end
class B
include Mongoid::Document
field :content
end
class B1 < B
belongs_to :a, class_name: "A1", inverse_of: :b
end
class B2 < B
belongs_to :a, class_name: "A2", inverse_of: :b
end
Here is what happen:
a1 = A1.new
a1.set_b!
a1.save!
#=> true
a1
#=> #<A1 _id: 511bf4e5e3e50aacb0000001, _type: "A1">
B1.count
#=> 1
a2 = A2.new
a2.set_b!
a2.save!
#=> true
a2
#=> #<A2 _id: 511bf55be3e50aacb0000003, _type: "A2">
B2.count
#=> 0
a2.b
#=> [#<B2 _id: 511bf55be3e50aacb0000004, _type: "B2", content: "foo", a_id: "511bf55be3e50aacb0000003">]
a2.b.first.persisted?
#=> false
a2.b.map(&:save!)
#=> true
B2.count
#=> 1
B2.last
#=> #<B2 _id: 511bf55be3e50aacb0000004, _type: "B2", content: "foo", a_id: "511bf55be3e50aacb0000003">
Any idea?
thx