Active Record bang methods

We prefer using Active Record bang methods like update!, create! or save! over their normal counterparts when we are not checking the return value of those methods.

For eg.

def create
  if post.author_present?
    render json: { message: 'Author already present' }
  else =!
    render json: { message: 'Author added' }

Using bang version makes sure that if any exception is raised while persisting the object, we will see it immediately. Using non-bang version of save will just swallow the result. We will never really know if the object really persisted or not.

This rule has an exception though.

We have lot of controller code where we check if object is saved or created or not. If the object is persisted, success message is shown and if not then error message is shown.

  puts "Success!"
  puts "Failed!"

In such cases, non-bang version of save should be used because we are explicitly checking whether the object is saved or not.