-
-
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
Rails/InverseOf false positive #5236
Comments
I'm reading this and it says:
So why do rubocop tell me to add |
I tried verifying some patterns, but it seems that 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
end
However, it does not seem to work with |
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.
See #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.
Fixes rubocop#5236 When using `Object#with_options`, `:inverse_of` works even if doesn't declare it directly in associations. ```ruby class Blog < ApplicationRecord with_options inverse_of: :blog do # good has_many :posts, -> { order(published_at: :desc) } end end ``` So if the association is in a block with `with_options`, this Cop check that options.
Fixes #5236 When using `Object#with_options`, `:inverse_of` works even if doesn't declare it directly in associations. ```ruby class Blog < ApplicationRecord with_options inverse_of: :blog do # good has_many :posts, -> { order(published_at: :desc) } end end ``` So if the association is in a block with `with_options`, this Cop check that options.
@delphaber AFAICS the documentation is wrong, I have a PR out to fix it: rails/rails#31446 |
Worth noting that |
That was already taken care of in the original implementation of the cop - if it doesn't work it's a bug. |
Same as #5235 but for InverseOf.
=>
The text was updated successfully, but these errors were encountered: