Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sampatbadhe/multi-db-app
Rails 7 demo app for demonstrating associations across databases with disable_joins
https://github.com/sampatbadhe/multi-db-app
rails7
Last synced: about 1 month ago
JSON representation
Rails 7 demo app for demonstrating associations across databases with disable_joins
- Host: GitHub
- URL: https://github.com/sampatbadhe/multi-db-app
- Owner: sampatbadhe
- Created: 2021-12-29T12:19:46.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-28T10:16:34.000Z (almost 3 years ago)
- Last Synced: 2024-11-09T16:57:42.461Z (2 months ago)
- Topics: rails7
- Language: Ruby
- Homepage:
- Size: 32.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Multi-Database Application
Sample Rails application for demonstrating [Rails 7 - Associations across databases with disable_joins](https://blog.kiprosh.com/rails7-association-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
```2. cd to the `passwordless-authentication-api` application directory
```
cd multi-db-app
```3. Run `bundle` command to install all gems
```
bundle install
```4. Configure your `database.yml` file.
5. Run `bundle exec rails db:create`
6. Run `bundle exec rails db:migrate`
7. Run `bundle exec rails db:seed`. The sample data would be then loaded into application database.
8. Run the rails console using `bundle exec rails console` or `bundle exec rails c`
9. 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.
```ruby
# Set the user
> user = User.find_by(email: '[email protected]')
# 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**
```ruby
# Set the user
> user = User.find_by(email: '[email protected]')
# 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
```