Giter Site home page Giter Site logo

kaspermeyer / rails-active-record-and-mongoid-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattwelke/rails-active-record-and-mongoid-example

0.0 1.0 0.0 262 KB

An example Rails app demonstrating using both ActiveRecord and Mongoid, including an association between two models, User and Post, each stored in different databases.

License: The Unlicense

JavaScript 3.70% Ruby 76.43% CSS 2.22% HTML 17.66%

rails-active-record-and-mongoid-example's Introduction

example-rails-active-record-and-mongoid

An example Rails app demonstrating using both ActiveRecord and Mongoid, including an association between two models stored in different databases.

User is an ActiveRecord model and Post is a Mongoid model. In this example, one user is associated with many posts.

Prepare for use

The project is set up to use CircleCI to help with Dependabot keeping the dependencies up to date. If you plan to not use CI or to use CI other than CircleCI, you can clean out CircleCI-related code from the project with the command rm -r .circleci from the project root.

Running

  1. Ensure the required gems are installed.
  2. Ensure MongoDB is running. If you have Docker and docker-compose installed, you can use docker-compose up to create a stack that includes MongoDB.

Example:

Create a user

bob = User.create(email: '[email protected]')

Create a post

greeting = Post.new(title: 'Hello', body: 'World')

Associate post with user

This sets the post's User foreign key to the user's primary key value (posts are the many side of a one-to-many relationship. It is not yet persisted in the database.

greeting.user = bob

Persist post

This persists the foreign key change in the database, permanantly associating the models.

greeting.save

Get the post's user

Returns type User because this is the one side of a one-to-many relationship.

greeting.user

Get the user's posts

Returns type Mongoid::Criteria because this is the many side of a one-to-many relationship.

bob.posts

This allows a Mongoid query to be chained to filter the posts.

bob.posts.find_by(title: 'Hello')

If the query targets only one post, this returns type Post.

Hitting both data stores

Note that some queries will perform synchronous reads from both data stores.

User.find_by(name: 'Bob').posts.where(title: 'Hello') # Hits Users in Postgres, then Posts in MongoDB

Consider storing references during controller actions to minimize data store hits.

Instead of doing this:

# Find bob's "hello" posts
hello_posts = User.find_by(name: 'Bob').posts.where(title: 'Hello')
# Then, find bob's "goodbye" posts
goodbye_posts = User.find_by(name: 'Bob').posts.where(title: 'Goodbye')

Do this:

# store reference to Bob
bob = User.find_by(name: 'Bob')

# now, only need to hit MongoDB for any posts for this user (won't hit Postgres again)
hello_posts = bob.posts.where(title: 'Hello')
goodbye_posts = bob.posts.where(title: 'Goodbye')

Details

See the model code in this repo to see how defining methods on each model that interact with the other data store's API links the models together, as if you were using just ActiveRecord or just Mongoid.

These concepts apply to more than just ActiveRecord and Mongoid. You can link models from any persistence mechanism, including databases, REST endpoints, memory caches, etc, by defining methods like these.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.