-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Improve inspection for Rails/InverseOf
#5297
Improve inspection for Rails/InverseOf
#5297
Conversation
Thanks for opening PR. #5236 reports on false positives with
If you don't resolve |
9747a4a
to
adee543
Compare
Rails/InverseOf
Rails/InverseOf
Thanks for the advice. I removed |
You are welcome. I think that writing a link to #5236 in the commit message is good for showing a practical issue. For example, I prefer In this case it would be better to show the issue without using the following keywords. |
See rubocop#5236 Currently, `Rails/InverseOf` Cop checks associations unspecified `inverse_of` and included `conditions`, `through`, `polymorphic`, or `foreign_key`. However, this inspection is incorrect. For example, for `through` and `polymorphic` you have to add `inverse_of` to the inverse association of them. ```ruby class Physician < ApplicationRecord has_many :appointments has_many :patients, through: :appointments, inverse_of: physicians end class Appointment < ApplicationRecord belongs_to :physician belongs_to :patient end class Patient < ApplicationRecord has_many :appointments has_many :physicians, through: :appointments, inverse_of: :patients end class Physician < ApplicationRecord has_many :appointments has_many :patients, through: :appointments end class Appointment < ApplicationRecord belongs_to :physician, inverse_of: :appointments belongs_to :patient, inverse_of: :appointments end class Patient < ApplicationRecord has_many :appointments has_many :physicians, through: :appointments end ``` ```ruby class Picture < ApplicationRecord belongs_to :imageable, polymorphic: true, inverse_of: :pictures end class Employee < ApplicationRecord has_many :pictures, as: :imageable end class Product < ApplicationRecord has_many :pictures, as: :imageable end class Picture < ApplicationRecord belongs_to :imageable, polymorphic: true end class Employee < ApplicationRecord has_many :pictures, as: :imageable, inverse_of: :imageable end class Product < ApplicationRecord has_many :pictures, as: :imageable, inverse_of: :imageable end ``` Unfortunately, even if this Cop looks at the structure of the `send` node, it can't check for `through` associations. So, it ignores them. In the case of `polymorphic`, this Cop should check on the inverse `as` associations. In addition, I added the check of `class_name` option. This Cop still includes false positives when using `Object#with_options`. Perhaps I will fix this problem as another pull request.
adee543
to
98640a4
Compare
See #5236
Currently,
Rails/InverseOf
Cop checks associations unspecifiedinverse_of
and includedconditions
,through
,polymorphic
, orforeign_key
. However, this inspection is incorrect.For example, for
through
andpolymorphic
you have to addinverse_of
to the inverse association of them.Unfortunately, even if this Cop looks at the structure of the
send
node, it can't check forthrough
associations. So, it ignores them. In the case ofpolymorphic
, this Cop should check on the inverseas
associations. In addition, I added the check ofclass_name
option.This Cop still includes false positives when using
Object#with_options
. Perhaps I will fix this problem as another pull request.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.
rake default
orrake parallel
. It executes all tests and RuboCop for itself, and generates the documentation.