-
-
Save ashmoran/1482621 to your computer and use it in GitHub Desktop.
The magical document eating has_many assignment (now incorporating the dark arts): https://github.com/mongoid/mongoid/issues/1503
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler' | |
Bundler.setup | |
require 'mongoid' | |
require 'rspec/autorun' | |
Mongoid.configure do |config| | |
config.master = Mongo::Connection.new.db("has_many_bug") | |
end | |
class Kid | |
include Mongoid::Document | |
belongs_to :parent | |
end | |
class Parent | |
include Mongoid::Document | |
has_many :kids | |
end | |
describe "Mongoid parent with has_many mass-assignment" do | |
subject { Parent.create! } | |
let(:kids) { (0..9).map { |i| Kid.create! } } | |
before(:each) do | |
Kid.destroy_all | |
Parent.destroy_all | |
subject.kids << kids | |
end | |
context "without reloading the parent" do | |
its("kids.count") { should be == 10 } | |
context "after assigning new Kids" do | |
before(:each) do | |
subject.kids = new_kids | |
end | |
context "from an array of existing elements" do | |
let(:new_kids) { Kid.all.to_a } # on_the_block | |
its("kids.count") { should be == 10 } | |
specify { Kid.count.should be == 10 } | |
end | |
context "from a collection proxy of existing elements" do | |
let(:new_kids) { Kid.all } | |
its("kids.count") { should be == 10 } | |
specify { Kid.count.should be == 10 } | |
end | |
context "from an array of new and old elements" do | |
let(:new_kids) { [ Kid.create! ] + Kid.all.take(3) + [ Kid.create! ] } | |
its("kids.count") { should be == 5 } # Failed: got 2 | |
specify { Kid.count.should be == 12 } | |
end | |
end | |
end | |
context "after reloading the parent" do | |
before(:each) do | |
subject.reload | |
end | |
its("kids.count") { should be == 10 } | |
context "after assigning new Kids" do | |
before(:each) do | |
subject.kids = new_kids | |
end | |
context "from an array of existing elements" do | |
let(:new_kids) { Kid.all.to_a } | |
its("kids.count") { should be == 10 } # Failed: got 0 | |
specify { Kid.count.should be == 10 } | |
end | |
context "from a collection proxy of existing elements" do | |
let(:new_kids) { Kid.all } | |
its("kids.count") { should be == 10 } | |
specify { Kid.count.should be == 10 } | |
end | |
context "from an array of new and old elements" do | |
let(:new_kids) { [ Kid.create! ] + Kid.all.take(3) + [ Kid.create! ] } | |
its("kids.count") { should be == 5 } # Failed: got 2 | |
specify { Kid.count.should be == 12 } | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a slightly modified version of the original gist to show a related bug | |
require 'bundler' | |
Bundler.setup | |
require 'mongoid' | |
Mongoid.configure do |config| | |
config.master = Mongo::Connection.new.db("document_eater") | |
end | |
class Kid | |
include Mongoid::Document | |
belongs_to :parent | |
end | |
class Parent | |
include Mongoid::Document | |
has_many :kids | |
end | |
Kid.destroy_all | |
Parent.destroy_all | |
p = Parent.create! | |
10.times do |n| | |
k = Kid.create! | |
p.kids << k | |
end | |
p.reload | |
puts "Parent has #{p.kids.count} kids" | |
kids = Kid.all.to_a | |
p = Parent.find(p.id) | |
p.kids = kids | |
puts "There are now #{Kid.count} kids" | |
# New line which shows a different bug from #1173 | |
puts "Parent has #{p.kids.count} kids" # Says 0, should be 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
➜ mongoid git:(master) ✗ ruby test.rb | |
Parent has 10 kids | |
There are now 10 kids | |
Parent has 0 kids |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment