Skip to content
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

Remove dependent error for has_many through #221

Closed
dsandstrom opened this issue Oct 19, 2017 · 2 comments
Closed

Remove dependent error for has_many through #221

dsandstrom opened this issue Oct 19, 2017 · 2 comments

Comments

@dsandstrom
Copy link

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
Copy link
Member

pirj commented Feb 21, 2021

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.

@pirj pirj closed this as completed Feb 21, 2021
@tsrivishnu
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants