Skip to content

Instantly share code, notes, and snippets.

@ryana
Forked from ahoward/3.1.6.txt
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryana/29d464f1c3f8cd6f6f2e to your computer and use it in GitHub Desktop.
Save ryana/29d464f1c3f8cd6f6f2e to your computer and use it in GitHub Desktop.
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