title: Why/ when to check for Object.persisted? versus Object.save
body: <p>I'm just trying to understand two patterns that I've seen in Ruby code.</p>
  
  <p>In Michael Hartl's standard tutorial, the code is as such:</p>
  
  <pre><code>def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end
  </code></pre>
  
  <p>This is the pattern I'm very used to. I just implemented Devise, and its pattern however, is this:</p>
  
  <pre><code>def create
    build_resource(sign_up_params)
  
    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_flashing_format?
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
    end
  end
  </code></pre>
  
  <p>Why is devise checking for <code>resource.persisted?</code> in the if statement as opposed to <code>resource.save</code>? And when might you want to use one over the other?</p>
  
  <p>Thanks!</p>