Skip to content

Instantly share code, notes, and snippets.

@ryana

ryana/a.rb

Forked from ahoward/3.1.6.txt
Last active Aug 29, 2015
Embed
What would you like to do?
require 'mongoid'
Mongoid.configure do |config|
config.connect_to('test')
end
class Room
include Mongoid::Document
has_one :table, as: :parent, autobuild: true
after_create :ensure_table
def ensure_table
table.save!
end
end
class Table
include Mongoid::Document
belongs_to :parent, polymorphic: true
embeds_many :chairs
end
class Chair
include Mongoid::Document
embedded_in :table
end
n = Table.count
m = 4
@room = Room.create!
@table = @room.table
@table.reload
m.times{ @table.chairs << Chair.new }
abort 'blargh' unless(Table.count == n + 1)
abort 'blurgh' unless(@table.chairs.size == m)
abort 'blurghy!' unless(Table.first.chairs.size == m)
puts Mongoid::VERSION #=> 3.1.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment