rubocop/rails-style-guide

Remove dependent error for has_many through

dsandstrom opened this issue · 2 comments

I'm getting hit with a linting error when I use has_many with the through option.

A trivial example:

has_many :users, dependent: :destroy
has_many :comments, through: :users # wants dependent: :destroy

I don't see a point to adding the dependent option in this case. Am I missing something? If I was using a join table, it might be helpful, but that is not the case.

pirj commented

I guess the linter is wrong, and if it's still the case, please open a ticket for https://github.com/rubocop-hq/rubocop-rails/
Two reasons:

  1. Users might or might not want to destroy such an association.
    Imagine the case of User -> Subscription -> Content. It would be horrible to destroy content when a User is destroyed with their subscriptions.

  2. If the business logic suggests that comments should be destroyed along with users, this should go to User model, and not be part of the association that triggered the destroy of User.

Is the linter really wrong here?

  1. Users might or might not want to destroy such an association.
    Imagine the case of User -> Subscription -> Content. It would be horrible to destroy content when a User is destroyed with their subscriptions.

According to Rails' documentation for has_many with :through, the dependent: :destroy option will only destroy the join models. In that case, Content is not deleted. Isn't it?