Skip to content

Instantly share code, notes, and snippets.

@bernerdschaefer
Created July 22, 2010 15:38
Show Gist options
  • Select an option

  • Save bernerdschaefer/486126 to your computer and use it in GitHub Desktop.

Select an option

Save bernerdschaefer/486126 to your computer and use it in GitHub Desktop.
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('testing')
end
class Product
include Mongoid::Document
field :name
references_many :categories, :stored_as => :array
collection.remove
end
class Category
include Mongoid::Document
field :name
collection.remove
end
category = Category.create(:name => 'Books')
product = Product.create(:name => 'Enduring Love')
product.categories << category
product.attributes
# => {"name"=>"Enduring Love", "_id"=>"4c48659bf7e2da1388000002", "category_ids"=>["4c48659bf7e2da1388000001"]}
#
category.attributes
# => {"name"=>"Books", "_id"=>"4c48659bf7e2da1388000001"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment