Skip to content

Instantly share code, notes, and snippets.

@svenyurgensson
Created May 27, 2011 07:31
Show Gist options
  • Save svenyurgensson/994803 to your computer and use it in GitHub Desktop.
Save svenyurgensson/994803 to your computer and use it in GitHub Desktop.
Mongoid bug
require "rubygems"
require "bson"
require "mongoid"
require 'ap'
Mongoid.configure do |config|
name = "test"
host = "localhost"
config.allow_dynamic_fields = false
config.master = Mongo::Connection.new.db(name)
end
class User
include Mongoid::Document
field :name, :type => String
has_many :own_projects, :class_name=>'Project', :dependent=>:destroy
end
class Project
include Mongoid::Document
field :title, :type => String
belongs_to :owner, :class_name=>"User", :foreign_key=>:user_id
embeds_many :logs do
def start(msg, uid)
@target << FirstLog.new(title: msg, uid: uid)
end
end
after_create :add_start
private
def add_start
self.logs.start("Project created", owner._id)
end
end
class Log
include Mongoid::Document
include Mongoid::Timestamps::Created
field :title, :type => String
field :uid, :type=>BSON::ObjectId
embedded_in :project
end
class FirstLog < Log; end
class SecondLog < Log; end
user = User.create(name: "Foo")
project = user.own_projects.create(title: "Proj1")
project.save
project.logs.start("start", user._id)
project.logs << SecondLog.new(title: "stop")
project.save
ap user
ap project
ap project.logs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment