Created
April 23, 2010 20:51
-
-
Save mharris717/377153 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lets you define relations between ActiveRecord and Mongo objects | |
module MongoRelations | |
module InstanceMethods | |
# get an object representing the MongoMapper equivalent of this object | |
# for the purpose of access relations on the object | |
fattr(:mongo_obj) do | |
res = klass.mongo_class.new | |
my_id = id | |
res.class_eval do | |
define_method(:id) do | |
my_id | |
end | |
end | |
res | |
end | |
end | |
module ClassMethods | |
# get an anonymous class representing the MongoMapper equivalent of this class | |
fattr(:mongo_class) do | |
Class.new do | |
include MongoMapper::Document | |
end | |
end | |
def mongo_has_many(name,ops={},&b) | |
define_method(name) do | |
mongo_obj.send(name) | |
end | |
mongo_class.send(:has_many, name, ops, &b) | |
end | |
end | |
def self.included(mod) | |
mod.send(:include,InstanceMethods) | |
mod.extend(ClassMethods) | |
end | |
end | |
class User < ActiveRecord::Base | |
mongo_has_many :workspaces, :foreign_key => :user_ids | |
end | |
class Workspace | |
include MongoMapper::Document | |
key :user_ids | |
key :name | |
end | |
User.first.workspaces.find('4bd0c170d489e6af74000130') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment