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

Rails/SaveBang: If you use a do block after a create... it won't recognize the .persisted? check. #3439

Closed
arianf opened this issue Aug 24, 2016 · 1 comment
Labels

Comments

@arianf
Copy link

arianf commented Aug 24, 2016

https://github.com/bbatsov/rails-style-guide#save-bang

If you use a do block after a create... it won't recognize the .persisted? check.


Expected behavior

No error

Actual behavior

Throws:

Rails/SaveBang: Use create! instead of create if the return value is not checked. (https://github.com/bbatsov/rails-style-guide#save-bang)

Steps to reproduce the problem

Turn on

Rails/SaveBang:
  Enabled: true

Then

bob = User.create do |user|
  user.name = name
end

if bob.persisted?
  logger.error 'yay'
else
  logger.error 'meow'
end

RuboCop version

Include the output of rubocop -V:

$ rubocop -V
0.42.0 (using Parser 2.3.1.2, running on ruby 2.1.1 x86_64-darwin16.
@QuinnHarris
Copy link
Contributor

QuinnHarris commented Sep 1, 2016

The current cop does not do any more than verify that the result of a method is saved in a variable. But this check does not work properly if a block was used as you show.
Actually checking if persisted? is checked is a good improvement I expect to PR soon.

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

No branches or pull requests

3 participants