Skip to content

Instantly share code, notes, and snippets.

@hashrocketeer
Created July 27, 2010 18:00
Show Gist options
  • Save hashrocketeer/492590 to your computer and use it in GitHub Desktop.
Save hashrocketeer/492590 to your computer and use it in GitHub Desktop.
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('testing')
end
class User
include Mongoid::Document
collection.remove
end
class Post
include Mongoid::Document
belongs_to_related :created_by, :class => "User"
collection.remove
end
p user = User.create
p post = Post.create(:created_by => user)
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('testing')
end
class User
include Mongoid::Document
references_many :posts, :stored_as => :array
collection.remove
end
class Post
include Mongoid::Document
collection.remove
end
p user = User.create
user.posts.create
p user.attributes
@bernerdschaefer
Copy link

Whoops. The association on post should look like this:

class Post
  belongs_to_related :created_by, :class_name => "User"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment