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.
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:
-
Users might or might not want to destroy such an association.
Imagine the case ofUser
->Subscription
->Content
. It would be horrible to destroy content when a User is destroyed with their subscriptions. -
If the business logic suggests that
comments
should be destroyed along withusers
, this should go toUser
model, and not be part of the association that triggered the destroy ofUser
.
Is the linter really wrong here?
- 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?