title: Undefined model method in rails 4
body: <p>I am trying to configure Google OAuth and I am facing a very silly mistake I can't solve :
  When I click on the Sign in button on the home page:</p>
  
  <pre><code>&lt;% if user_signed_in? %&gt;
    Signed in as &lt;%= current_user.name %&gt;. Not you?
    &lt;%= link_to "Sign out", destroy_user_session_path,:method =&gt; :delete %&gt;
  &lt;% else %&gt;
    &lt;%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I get this error undefined method <code>find_for_google_oauth2</code> for #</p>
  
  <p>It says the problem is at that line: 
  <code>@user = user.find_for_google_oauth2(request.env["omniauth.auth"], current_user)</code></p>
  
  <p>Here is my <code>app/controllers/users/omniauth_callbacks_controller.rb</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>class Users::OmniauthCallbacksController &lt; Devise::OmniauthCallbacksController
  
    def facebook
      # You need to implement the method below in your model (e.g. app/models/user.rb)
      @user = User.from_omniauth(request.env["omniauth.auth"])
      if @user.persisted?
        sign_in_and_redirect @user, :event =&gt; :authentication #this will throw if @user is not activated
        set_flash_message(:notice, :success, :kind =&gt; "Facebook") if is_navigational_format?
      else
        session["devise.facebook_data"] = request.env["omniauth.auth"]
        redirect_to new_user_registration_url
      end
    end
  
    def google_oauth2
      @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
      if @user.persisted?
        flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind =&gt; "Google"
        sign_in_and_redirect @user, :event =&gt; :authentication
      else
        session["devise.google_data"] = request.env["omniauth.auth"]
        redirect_to new_user_registration_url
      end
    end
  end</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is my app/models/user.rb:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>class User &lt; ActiveRecord::Base
    before_save :default_values
    attr_accessor :delete_photo
    validates :first_name, presence: true
    validates :last_name, presence: true
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers =&gt; [:facebook,:google_oauth2]
    has_many :usertoskills
    has_many :skills, through: :usertoskills
    has_many :usertoprojects
    has_many :projects, through: :usertoprojects
    has_many :demands
    has_many :news
    has_attached_file :photo, :styles =&gt; { :small =&gt; "150x150&gt;", :regular =&gt; "300x300&gt;" },
                    :url  =&gt; "/assets/users/:id/:style/:basename.:extension",
                    :path =&gt; ":rails_root/public/assets/users/:id/:style/:basename.:extension",
                    :default_url =&gt; "/assets/users/default/:style/default.jpg"
    validates_attachment_content_type :photo, :content_type =&gt; ['image/jpeg', 'image/png']
    #def name
    #  "#{self.first_name.camelize} #{self.last_name.camelize}"
    #end
    def default_values
      if self.first_name &amp;&amp; self.last_name
        self.name = "#{self.first_name.camelize} #{self.last_name.camelize}"
      end
    end
  
    def admin
      self.role == 'admin'
    end
  
    def accessible_demands
      if self.role == 'admin'
        @demands = Demand.all
      else
        @demands = []
        self.projects.each do |p| @demands.concat(p.demands) end
        @demands = Demand.includes(:project).where(projects: { collective: true }, demands: {user_id: self.id}) | @demands
      end
      return @demands
    end
  
    def accessible_projects
      if self.role == 'admin'
        @projects = Project.all
      else
        @projects = self.projects
      end
      return @projects
    end
  
    def accessible_transactions
      if self.role == 'admin'
        @transactions = Transaction.all
      else
        @transactions = Transaction.where(sender_id: self.id)
      end
    end
  
    def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      #user.first_name = auth.info.first_name
      #user.last_name = auth.info.last_name   # assuming the user model has a name
      #user.image = auth.info.image # assuming the user model has an image
    end
  
    def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
      data = access_token.info
      user = User.where(:provider =&gt; access_token.provider, :uid =&gt; access_token.uid ).first
      if user
        return user
      else
        registered_user = User.where(:email =&gt; access_token.info.email).first
        if registered_user
          return registered_user
        else
          user = User.create(name: data["name"],
            provider:access_token.provider,
            email: data["email"],
            uid: access_token.uid ,
            password: Devise.friendly_token[0,20],
          )
        end
     end
   end
  end
  
  end</code></pre>
  </div>
  </div>
  </p>
  
  <p>Thanks in advance for your help!</p>