-
-
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
Revert "[Fix #5426] Make Rails/InverseOf
accept inverse_of: nil
to opt-out (#5430)
#5730
Conversation
Hi @bdewater, Thank you for this useful cop. Considering the implementation of Example (Rails v5.1.4): # app/models/author.rb
class Author < ApplicationRecord
has_many :books
end
# app/models/book.rb
class Book < ApplicationRecord
belongs_to :author
end
# app/models/author.rb
class Author < ApplicationRecord
has_many :books, inverse_of: nil
end
# app/models/book.rb
class Book < ApplicationRecord
belongs_to :author
end
Looking at this behavior, I guess that it is not unnatural to be able to use WDYT? |
Hi @wata727! Thank you for your useful additions to the cop 😄 Yes the end-result is the same in your example, but how we get there is different since Which is why I chose to follow exactly what the code told me. I also noticed that the documentation and the tests all use |
Thank you for your reply! I agree with your opinion. This change should be reverted. |
lib/rubocop/cop/rails/inverse_of.rb
Outdated
# class Blog < ApplicationRecord | ||
# has_many(:posts, | ||
# -> { order(published_at: :desc) }, | ||
# inverse_of: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to leave this example :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 good call!
f54dc5a
to
85d14f2
Compare
@bdewater I use Mongoid, which allows nil. |
Right, but Mongoid is not ActiveRecord or part of Rails. I've never used it so I can't even say if the assumptions the cop makes in |
The changes look good, but your branch has to be rebased on top of the current On a related note - it might be good to add some other check for The documentation of this cop can be extended a bit as well. |
@bbatsov I rebased and addressed your comments 👍 |
… nil` to opt-out (rubocop#5430)" This partially reverts commit b12978c. ActiveRecord specifically checks for `false`, not a falsey value, to stop evaluating if the inverse can be found automatically: https://github.com/rails/rails/blob/d35875b7a3f559155a9378cbe9203b0b8ea580f9/activerecord/lib/active_record/reflection.rb#L647-L652
Thanks! |
This reverts commit b12978c. /cc @wata727 @johnnyshields
ActiveRecord specifically checks for
false
, not a falsey value, to stop evaluating if the inverse can be found automatically:https://github.com/rails/rails/blob/d35875b7a3f559155a9378cbe9203b0b8ea580f9/activerecord/lib/active_record/reflection.rb#L647-L652
Before submitting the PR make sure the following are checked:
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.