-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Description
I am not sure whether it should be reported as style guide problem, or rubocop-rails problem, but it is a problem :)
Basically, the styleguide claims and the cop supports this claim, that those statements are equivalent:
User.all.<anymethod>
# and
User.<anymethod>
user.articles.all.<anymethod>
# and
user.articles.<anymethod>
The latter is incorrect at least in a case of delete_all
(which I shoot myself in a foot with just recently, after update to the latest rubocop-rails):
user.articles.all.delete_all
is ActiveRecord::Relation#delete_all which will just do SQL DELETE, done and forgotten;- but
user.articles.delete_all
is ActiveRecord::Associations::CollectionProxy#delete_all which behaves "according to a deletion strategy", which, say, forhas_many
is by default:nullify
.
E.g.:
user.articles.all.delete_all
=DELETE FROM articles WHERE user_id=1
(delete articles in this scope)user.articles.delete_all
=UPDATE articles WHERE user_id=1 SET user_id=NULL
(remove articles of this scope from the collection "articles of this user")
Which is... Quite a difference. I love Rails and its conventions 😍
So... Probably both the style guide and the cop should be much more cautious here.
pirj
Metadata
Metadata
Assignees
Labels
No labels