Skip to content

Instantly share code, notes, and snippets.

@mkelley33
Created August 16, 2013 23:46
Show Gist options
  • Save mkelley33/6254465 to your computer and use it in GitHub Desktop.
Save mkelley33/6254465 to your computer and use it in GitHub Desktop.
Mongoid.connect_to 'mongoid-3144'
class Post
include Mongoid::Document
field :name
embeds_many :comments
accepts_nested_attributes_for :comments
# This line somehow triggers the bug; commenting it out fixes the bug, wierd.
has_many :children, class_name: 'Item', foreign_key: :parent_id, inverse_of: :parent
# This on it's own doesn't cause the bug.
belongs_to :parent, class_name: 'Item', inverse_of: :children, index: true
end
class Comment
include Mongoid::Document
embedded_in :post
field :title
end
post = Post.create({
"name" => "A post",
"comments_attributes" => {
"0" => { "title" => "Comment 1" }
}
})
post.reload
post.update({
"name" => "A post!",
"comments_attributes" => {
"0" => { "title" => "Comment 1!", "id" => post.comments.first.id },
"12345" => { "title" => "New Comment 2!" }
}
})
puts
puts "The following counts should both be 2 but the last comment is not"
puts "being persisted. Commenting out the 'has_many :children' line in the"
puts "Post model fixes the bug, WTF?"
puts
puts " * count: #{post.comments.count}, comments: " + post.comments.map(&:title).inspect
post.reload
puts " * count: #{post.comments.count}, comments: " + post.comments.map(&:title).inspect
puts
Post.destroy_all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment