Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Rails/StrictLoadingAssociations cop #876

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Drenmi
Copy link
Contributor

@Drenmi Drenmi commented Nov 28, 2022

Rails 6.1 added a set of association strict loading tools to prevent N+1 queries. One of these is the ability to declare a has_many association to only strict load (failing to do so will raise an exception.) Reference.

Example:

class Author < ApplicationRecord
  has_many :posts, strict_loading: true
end

This PR adds a cop that enforces declaring a strict_loading option on has_many associations when using Rails 6.1 or newer. It is not concerned with what the value is, as setting this to true across the board is likely not good either.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@Drenmi Drenmi force-pushed the feature/strict-loading-associations-cop branch 2 times, most recently from 6f0d5b2 to e77e58f Compare November 28, 2022 02:55
@Drenmi Drenmi force-pushed the feature/strict-loading-associations-cop branch from e77e58f to 6691344 Compare November 28, 2022 03:00

def on_send(node)
has_many_association(node) do |options|
add_offense(node) unless strict_loading_declared?(options.first)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit surprised that $hash ? produces an array with 0 or 1 elements, instead of nil or match. 🙂

@koic
Copy link
Member

koic commented Jan 27, 2023

AFAIK, strict_loading has the following approaches in addition to defining in association.

  • Specify in the application config: config.active_record.strict_loading_by_default = true
  • Specify in model units: self.strict_loading_by_default = true
  • Use as a method: Model.strict_loading.first

I'm not sure it's better approach to always force strict_loading to associations without considering these use cases.

@koic
Copy link
Member

koic commented Apr 5, 2023

@Drenmi Can you handle the above? I'm concerned that the lack of the above points could possibly cause user confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants