title: Rails3 devise / omniauth - password_required? and form_for :validate => true don't work together
body: <p>I'm using devise, omniauth and client-side-validation like described in ryan bates railscasts.</p>
  
  <p>I'm now facing a problem with the password validation which should be omitted when registering via omniauth 3rd party provider.</p>
  
  <p><strong>Registration form (html.erb):</strong></p>
  
  <blockquote>
    <p><h2>Sign up</h2>   <h3>Register
    via 3rd party networks</h3>    &lt;%= form_for(resource, :as => resource_name, :url =>
    registration_path(resource_name), :validate => true) do |f| %>
    <h3>Register directly</h3> <p>&lt;%= f.label :firstname %> &lt;%=
    f.text_field :firstname %></p> <p>&lt;%= f.label :lastname %> &lt;%=
    f.text_field :lastname %></p> <p>&lt;%= f.label :email %> &lt;%=
    f.text_field :email %></p> <p>&lt;%= f.label :password %> &lt;%=
    f.password_field :password %></p> <p>&lt;%= f.label
    :password_confirmation %> &lt;%= f.password_field :password_confirmation
    %></p> <p>&lt;%= f.submit "Register now", :class => "button-big" %></p>
    &lt;% end %>  </p>
  </blockquote>
  
  <p><strong>My user model has a - see UPDATE below</strong></p>
  
  <pre><code>validates :password, :presence =&gt; true, :confirmation =&gt;true
  </code></pre>
  
  <p><strong>clause and the password_required? definition</strong></p>
  
  <pre><code>def password_required?
  (authentications.empty? || !password.blank?)
  end
  </code></pre>
  
  <p>When I'm registering via omniauth 3rd party provider the registration form correctly pops up and the user is asked to enter an email address. Unfortunately the user has to enter a password although he shouldn't be prompted due to registration via 3rd party.</p>
  
  <p>Can anybody give me a hint, how to accomplish?</p>
  
  <p>Thanks and best regards</p>
  
  <p>Jan </p>
  
  <p>UPDATE: to give a more specific view I added some more code snippets</p>
  
  <h1>AuthenticationsController:</h1>
  
  <pre><code>class AuthenticationsController &lt; ApplicationController
  def index
  @authentications = current_user.authentications if current_user
  end
  
  def create
  omniauth = request.env["omniauth.auth"]
  authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
  if authentication
  flash[:notice] = "Signed in successfully."
  sign_in_and_redirect(:user, authentication.user)
  elsif current_user
  current_user.authentications.create(:provider =&gt; omniauth['provider'], :uid =&gt; omniauth['uid'])
  flash[:notice] = "Authentication successful."
  redirect_to authentications_url
  else
  user = User.new
  user.apply_omniauth(omniauth)
  if user.save
  flash[:notice] = "Signed in successfully."
  sign_in_and_redirect(:user, user)
  else
  session[:omniauth] = omniauth.except('extra')
  redirect_to new_user_registration_url
  end
  end
  end
  
  def destroy
  @authentication = current_user.authentications.find(params[:id])
  @authentication.destroy
  flash[:notice] = "Successfully destroyed authentication."
  redirect_to authentications_url
  end
  
  end
  </code></pre>
  
  <p>RegistrationsController:
      class RegistrationsController &lt; Devise::RegistrationsController</p>
  
  <pre><code>def create
  super
  session[:omniauth] = nil unless @user.new_record? 
  end
  
  private
  def build_resource(*args)
  super
  if session[:omniauth]
  @user.apply_omniauth(session[:omniauth])
  @user.valid?
  end
  end
  
  end
  </code></pre>
  
  <p>User Model:
      class User &lt; ActiveRecord::Base
      # associations
      has_many :authentications</p>
  
  <pre><code># Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, 
  :validatable, :email_regexp =&gt;  /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
  
  # Setup accessible (or protected) attributes for your model
  attr_accessible :firstname, :lastname, :email, :password, :password_confirmation, :remember_me
  
  # validations
  validates :firstname, :presence =&gt; true
  validates :lastname, :presence =&gt; true
  
  validates :email, :presence =&gt; true, :uniqueness =&gt; true
  validates_email :email
  
  validates :password, :presence =&gt; true, :confirmation =&gt;true            
  
  # omniauth reference 3rd party
  def apply_omniauth(omniauth)
  if self.firstname.blank?
  self.firstname = omniauth['info']['first_name'].presence || omniauth['info']['name'].presence || " "
  end
  
  if self.lastname.blank?
  self.lastname = omniauth['info']['last_name'].presence || omniauth['info']['name'].presence || " "
  end
  
  if self.email.blank?
  self.email = omniauth['info']['email'].presence
  end
  
  authentications.build(:provider =&gt; omniauth['provider'], :uid =&gt; omniauth['uid'])
  end
  
  # omit validation for omniauth session
  def password_required?
  (authentications.empty? || !password.blank?) &amp;&amp; super
  end
  
  end
  </code></pre>
  
  <p>Authentication Model
      class Authentication &lt; ActiveRecord::Base
      # associations
      belongs_to :user</p>
  
  <pre><code># Setup accessible (or protected) attributes for your model  
  attr_accessible :user_id, :provider, :uid
  end
  </code></pre>
  
  <p>While debugging I found out that the 'authentications.build()' in the 'apply_omniauth(omniauth)' method produces an empty object, so that the 'password_required?' is always be true and a password must be provided.</p>
  
  <p>ADDITIONAL QUESTION:
  why does 'authentications.empty?' always return 'false'?</p>
  
  <p>Thanks in advance</p>
  
  <p>Jan</p>