Mongoid::Activerecord::Eagerloadable

Belongs to relationships between Mongoid and ActiveRecord models. With eager loading.

Installation

Add this line to your application's Gemfile:

gem 'mongoid-activerecord-eagerloadable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid-activerecord-eagerloadable

Usage

Consider the following Mongoid document:

class Pet
  include Mongoid::Document
  include Mongoid::ActiveRecord::EagerLoadable

  belongs_to_active_record :person
end

And the following ActiveRecord class:

class Person < ActiveRecord::Base
  attr_accessible :name
end

The belongs_to_active_record :person statement enables the following to execute a single query against your mongodb instance and a single query against your ActiveRecord connection:

Pet.scoped.includes_active_record(:person).all.map(&:person)

Also, the person method is added to each instance of Pet regardless of whether or not the includes_active_record method is invoked

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request