Skip to content

Instantly share code, notes, and snippets.

@kyanny
Created November 28, 2016 18:08
Show Gist options
  • Save kyanny/42d961b252a5f1aca797f8482d959ca5 to your computer and use it in GitHub Desktop.
Save kyanny/42d961b252a5f1aca797f8482d959ca5 to your computer and use it in GitHub Desktop.
gem 'activemodel', '4.2.7.1'
require 'active_model'
require 'mongo_mapper'
require 'byebug'
require 'logger'
logger = Logger.new(STDOUT)
logger.level = :info
MongoMapper.setup(
{
'development' => {
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'testing',
}
},
'development',
logger: logger
)
class User
include MongoMapper::Document
one :membership
end
class Membership
include MongoMapper::Document
belongs_to :user
end
class WrapUser < User
one :membership, foreign_key: :user_id
end
wrap_user = WrapUser.create!
membership = Membership.create!(user: wrap_user)
wrap_user.reload
MongoMapper.connection.logger.level = :debug
p wrap_user.membership
@kyanny
Copy link
Author

kyanny commented Nov 28, 2016

$ ruby mm.rb
D, [2016-11-29T03:08:25.610934 #82282] DEBUG -- : MONGODB (0.3ms) testing['memberships'].find({:user_id=>BSON::ObjectId('583c72996200b0416a000001')}).limit(-1)
#<Membership _id: BSON::ObjectId('583c72996200b0416a000002'), user_id: BSON::ObjectId('583c72996200b0416a000001')>

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