Skip to content

Rails 7 demo app for demonstrating associations across databases with disable_joins

Notifications You must be signed in to change notification settings

sampatbadhe/multi-db-app

Repository files navigation

Multi-Database Application

Sample Rails application for demonstrating Rails 7 - Associations across databases with disable_joins

  • Ruby version: 2.7.1

  • Rails version: 7.0.0

Installation/Setup

  1. Clone the application on your local
git clone https://github.com/sampatbadhe/multi-db-app.git
  1. cd to the passwordless-authentication-api application directory
  cd multi-db-app
  1. Run bundle command to install all gems
  bundle install
  1. Configure your database.yml file.

  2. Run bundle exec rails db:create

  3. Run bundle exec rails db:migrate

  4. Run bundle exec rails db:seed. The sample data would be then loaded into application database.

  5. Run the rails console using bundle exec rails console or bundle exec rails c

  6. Run following operations and observe the queries.

    • Without disable_joins option

      We would have to add custom methods, as has_many :through/has_one :through associations won't work across databases.

      # Set the user
      > user = User.find_by(email: 'luke@example.com')
      # Fetch the messages of all the events of a user
      > user.event_messages
      # Fetch the latest message on the latest event of a user
      > user.latest_event_message
    • With disable_joins option

      # Set the user
      > user = User.find_by(email: 'luke@example.com')
      # We can use `has_many :through` association to fetch the messages of all the events of a user
      > user.messages
      # We can use `has_one :through` association to fetch the latest message on the latest event of a user
      > user.latest_message

About

Rails 7 demo app for demonstrating associations across databases with disable_joins

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published