Skip to content

Instantly share code, notes, and snippets.

@xuncheng
Created September 7, 2015 10:13
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 xuncheng/cf1a0352e7c0d0849072 to your computer and use it in GitHub Desktop.
Save xuncheng/cf1a0352e7c0d0849072 to your computer and use it in GitHub Desktop.
# source: http://hashrocket.com/blog/posts/bridging-activerecord-and-mongoid
module Extensions::ActiveRecord::HasManyDocuments
extend ActiveSupport::Concern
module ClassMethods
def has_many_documents(association_name, options = {})
class_eval %<
def #{association_name}
#{association_name.to_s.singularize.classify}.where(#{name.underscore}_id: id)
end
>
end
end
end
ActiveRecord::Base.send(:include, Extensions::ActiveRecord::HasManyDocuments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment