-
Type: Improvement
-
Resolution: Fixed
-
Priority: Minor - P4
-
Affects Version/s: None
-
Component/s: Docs
-
Fully Compatible
In the following example where _id field is overridden on both ends of HABTM with a default value, Mongoid writes nil instead of the _id value into the model that is created first as the foreign key value:
task :test_3267_1 => :environment do class Peach include Mongoid::Document has_and_belongs_to_many :pits field :_id, type: String, default: -> { 'hello' } end class Pit include Mongoid::Document has_and_belongs_to_many :peaches field :_id, type: String, default: -> { 'hello' } end Peach.delete_all Pit.delete_all a = Peach.new b = Pit.new(peaches: [a]) a.save! b.save! p Peach.last p Pit.last a.pits << b p '' p Peach.last p Pit.last end
Result:
#<Peach _id: hello, pit_ids: [nil]> #<Pit _id: hello, peach_ids: ["hello"]> "" #<Peach _id: hello, pit_ids: [nil, "hello"]> #<Pit _id: hello, peach_ids: ["hello"]>
Note that the nil value stays even after the correct _id is written later via the << operator.
- related to
-
MONGOID-3267 Allow associations to be used inside defaults
- Closed
- links to