0

我将 Authlogic 用于身份验证用户 + Omniauth 用于社交身份验证。为注册 Facebook 的用户创建新帐户后,我想在系统中设置他的角色。

用户模型

 def self.create_user_from_facebook(auth_hash)
    a = self.create!({
      :facebook_uid => auth_hash["uid"],
      ...,
      :crypted_password => "facebook",
      :password_salt => "facebook",
      :persistence_token => "facebook",
      :provider => auth_hash["provider"]
    })
    puts a.errors.inspect #returns no errors, record is successfully saved
    puts a.inspect # returns: last_request_at: "2012-10-01 08:12:51"
    Assignment.create(:user_id => user.id, :role_id => 2)
  end

当我尝试以assignments这种方式将新记录添加到表中时,出现错误

undefined method `last_request_at' for #<Assignment:0x0000012a1539e0>

该列last_request_at在表中users

last_request_at表格中的列users与表格有何关系assignments?(只有这两列:role_iduser_id)?

**EDIT** another information:
class UserOauthController < ApplicationController
  def create
    @current_user = User.find_or_create_from_oauth(auth_hash)

    if current_user
      UserSession.create(current_user, true)    
      flash[:success] = 'Successfully logged in.'
      redirect_to root_url
    else
      flash[:warning] = 'Authorization ERROR! Please, try it again.'
      redirect_to root_url
    end
  end
end

class User < ActiveRecord::Base
  attr_accessible list of atteibutes

  acts_as_authentic do |c|
    authlogic set up
  end

  ...validations...

  def self.find_or_create_from_oauth(auth_hash)
    provider = auth_hash["provider"]
    uid = auth_hash["uid"].to_s

    case provider      
      when 'facebook'
        if user = self.where('email = ? and provider = ?', auth_hash["info"]["email"], provider).first    
          return user
        elsif user = self.find_by_facebook_uid(uid)
          return user
        else
          return self.create_user_from_facebook(auth_hash)
        end

    end

    def self.create_user_from_facebook(auth_hash)
      a = self.create!({
        :facebook_uid => auth_hash["uid"],
        ...,
        :crypted_password => "facebook",
        :password_salt => "facebook",
        :persistence_token => "facebook",
        :provider => auth_hash["provider"]
      })
      puts a.errors.inspect
      puts a.inspect
      a.assignments.create(:role_id => MEMBER)
    end
  end

我该如何解决?

4

0 回答 0