1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/guides/source
eileencodes de6b4efa3e
Add option to skip joins for associations.
In a multiple database application, associations can't join across
databases. When set, this option tells Rails to make 2 or more queries
rather than using joins for associations.

Set the option on a has many through association:

```ruby
class Dog
  has_many :treats, through: :humans, disable_joins: true
  has_many :humans
end
```

Then instead of generating join SQL, two queries are used for `@dog.treats`:

```
SELECT "humans"."id" FROM "humans" WHERE "humans"."dog_id" = ?  [["dog_id", 1]]
SELECT "treats".* FROM "treats" WHERE "treats"."human_id" IN (?, ?, ?)  [["human_id", 1], ["human_id", 2], ["human_id", 3]]
```

This code is extracted from a gem we use internally at GitHub which
means the implementation here is used in production daily and isn't
experimental.

I often get the question "why can't Rails do this automatically" so I
figured I'd include the answer in the commit. Rails can't do this
automatically because associations are lazily loaded. `dog.treats` needs
to load `Dog`, then `Human` and then `Treats`. When `dog.treats` is
called Rails pre-generates the SQL that will be run and puts that
information into a reflection object. Because the SQL parts are pre-generated,
as soon as `dog.treats` is loaded it's too late to skip a join. The join
is already available on the object and that join is what's run to load
`treats` from `dog` through `humans`. I think the only way to avoid setting
an option on the association is to rewrite how and when the SQL is
generated for associations which is a large undertaking. Basically the
way that Active Record associations are designed, it is currently
impossible to have Rails figure out to not join (loading the association
will cause the join to occur, and that join will raise an error if the
models don't live in the same db).

The original implementation was written by me and Aaron. Lee helped port
over tests, and I refactored the extraction to better match Rails style.

Co-authored-by: Lee Quarella <leequarella@gmail.com>
Co-authored-by: Aaron Patterson <aaron@rubyonrails.org>
2021-04-19 11:17:31 -04:00
..
kindle Use image/jpeg instead of non-standard image/jpg 2021-03-05 13:14:11 -05:00
2_2_release_notes.md chore: fix spelling 2021-04-15 15:49:48 +10:00
2_3_release_notes.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
3_0_release_notes.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
3_1_release_notes.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
3_2_release_notes.md chore: fix case of CSS and JavaScript 2021-04-15 21:52:58 +10:00
4_0_release_notes.md Rename master to main in all code references 2021-01-19 20:46:33 +00:00
4_1_release_notes.md docs: fix missing HTTPS on links [ci-skip] 2021-04-12 09:45:13 +10:00
4_2_release_notes.md docs: fix missing HTTPS on links [ci-skip] 2021-04-12 09:45:13 +10:00
5_0_release_notes.md docs: fix grammar and spelling [ci skip] 2021-04-15 18:51:29 +10:00
5_1_release_notes.md docs: fix missing HTTPS on links [ci-skip] 2021-04-12 09:45:13 +10:00
5_2_release_notes.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
6_0_release_notes.md
6_1_release_notes.md Fix typo in 6.1 release notes 2021-03-22 21:15:17 +00:00
7_0_release_notes.md Fix link to Upgrading guide from 7.0 Release Notes guide [ci skip] 2021-02-25 18:25:30 -03:00
_license.html.erb
_welcome.html.erb Rails 6.2 is now Rails 7.0 2021-02-04 16:47:16 +00:00
action_cable_overview.md Merge pull request #41945 from jbampton/fix-grammar 2021-04-14 09:19:15 +09:00
action_controller_overview.md chore: fix grammar and spelling in Markdown and Ruby files 2021-04-15 19:32:27 +10:00
action_mailbox_basics.md chore: fix grammar and spelling in Markdown and Ruby files 2021-04-15 19:32:27 +10:00
action_mailer_basics.md fix on guides mailer_basics avoid ActiveModel::ForbiddenAttributesError 2021-03-31 10:51:10 -04:00
action_text_overview.md Fix HTML tag mismatches in Action Text README [ci skip] 2021-04-12 17:42:13 -04:00
action_view_helpers.md Lint Markdown blank lines around headings [ci-skip] 2021-04-14 01:17:46 +10:00
action_view_overview.md Lint Markdown blank lines around headings [ci-skip] 2021-04-14 01:17:46 +10:00
active_job_basics.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
active_model_basics.md
active_record_basics.md use proper styling name (snake_form) for table naming convention 2021-02-20 17:15:02 -05:00
active_record_callbacks.md
active_record_encryption.md Lint Markdown blank lines around headings [ci-skip] 2021-04-14 01:17:46 +10:00
active_record_migrations.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
active_record_multiple_databases.md Add option to skip joins for associations. 2021-04-19 11:17:31 -04:00
active_record_postgresql.md docs: fix missing HTTPS on links [ci-skip] 2021-04-12 09:45:13 +10:00
active_record_querying.md Lint Markdown blank lines around headings [ci-skip] 2021-04-14 01:17:46 +10:00
active_record_validations.md Add validate numericality in range 2021-01-05 22:56:58 +01:00
active_storage_overview.md Document that S3 requires s3:PutObjectAcl for public access [ci skip] 2021-04-14 14:13:07 -04:00
active_support_core_extensions.md chore: fix grammar and spelling 2021-04-12 05:30:44 +10:00
active_support_instrumentation.md Provide context when logging unpermitted parameters 2021-03-31 17:15:23 -06:00
api_app.md Remove Rack::Runtime and deprecate referencing it 2021-04-12 14:31:12 -04:00
api_documentation_guidelines.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
asset_pipeline.md Remove legacy media=screen default from stylesheet_link_tag. 2021-01-22 20:42:22 -03:00
association_basics.md Lint Markdown blank lines around headings [ci-skip] 2021-04-14 01:17:46 +10:00
autoloading_and_reloading_constants.md Drops support for classic mode 2021-03-08 05:30:11 +01:00
autoloading_and_reloading_constants_classic_mode.md Fix grammar 2021-01-05 12:36:47 -06:00
caching_with_rails.md chore: fix spelling 2021-04-15 15:49:48 +10:00
command_line.md Document input format for multiple arguments 2021-04-15 05:47:03 +01:00
configuring.md Copy edit #41718 2021-04-15 23:34:30 +00:00
contributing_to_ruby_on_rails.md docs: lint Markdown for four rules 2021-04-14 00:36:03 +10:00
debugging_rails_applications.md Lint Markdown blank lines around headings [ci-skip] 2021-04-14 01:17:46 +10:00
development_dependencies_install.md chore: fix case of CSS and JavaScript 2021-04-15 21:52:58 +10:00
documents.yaml Encryption guide (WIP) 2021-04-01 15:02:13 +02:00
engines.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
form_helpers.md Clarify when enctype="multipart/form-data" gets added to forms [docs] 2021-03-10 13:09:46 -06:00
generators.md Fix doc: stylesheet_include_tag -> stylesheet_link_tag 2021-01-23 13:07:58 -03:00
getting_started.md Update the "getting started" guide to Rails 6.1 2021-04-14 18:38:09 +02:00
i18n.md Remove reference to globalize gem 2021-01-24 20:10:09 +01:00
index.html.erb
initialization.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
layout.html.erb Rename master to main in all code references 2021-01-19 20:46:33 +00:00
layouts_and_rendering.md chore: fix case of CSS and JavaScript 2021-04-15 21:52:58 +10:00
maintenance_policy.md Clarify maintenance policy for bug fixes to security patches 2021-03-12 21:26:44 +00:00
plugins.md docs: fix grammar and spelling [ci skip] 2021-04-15 18:51:29 +10:00
rails_application_templates.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
rails_on_rack.md Remove Rack::Runtime and deprecate referencing it 2021-04-12 14:31:12 -04:00
routing.md docs: fix missing HTTPS on links [ci-skip] 2021-04-12 09:45:13 +10:00
ruby_on_rails_guides_guidelines.md
security.md Merge pull request #41945 from jbampton/fix-grammar 2021-04-14 09:19:15 +09:00
testing.md Lint Markdown blank lines around headings [ci-skip] 2021-04-14 01:17:46 +10:00
threading_and_code_execution.md Fix typos, grammar, and formatting [ci-skip] 2020-12-28 12:05:53 -06:00
upgrading_ruby_on_rails.md Add redirect_back directive to Rails 5.0 -> 5.1 upgrade guide 2021-02-19 08:39:18 +11:00
webpacker.md docs: fix grammar and spelling [ci skip] 2021-04-15 18:51:29 +10:00
working_with_javascript_in_rails.md docs: fix grammar and spelling [ci skip] 2021-04-15 18:51:29 +10:00