2012-07-01 64 views
1

我收到此錯誤是越來越創建一個新的用戶帳戶時:的SQLite3 :: BusyException錯誤

ActiveRecord::StatementInvalid in SessionsController#create 

SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction 

我使用的考拉和omniauth寶石autenticate並獲取用戶的朋友。爲什麼不能啓動兩個事務,爲什麼它會回滾朋友遷移?

如何解決這個問題?

這是我SessionController:

class SessionsController < ApplicationController 
    def create 
    user = User.from_omniauth(env['omniauth.auth']) 
    session[:user_id] = user.id 
    redirect_to root_url, notice: "Signed in!" 
    end 

這是我的用戶模型:

class User < ActiveRecord::Base 
    has_many :friends 

    def self.from_omniauth(auth) 
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user| 
     user.provider = auth["provider"] 
     user.uid = auth["uid"] 
     user.name = auth["info"]["name"] unless auth["info"].blank? 
     user.first_name = auth["info"]["first_name"] unless auth["info"].blank? 
     user.last_name = auth["info"]["last_name"] unless auth["info"].blank? 
     user.image = auth["info"]["image"] unless auth["info"].blank? 
     user.email = auth["info"]["email"] unless auth["info"].blank? 
     user.gender = auth["extra"]["raw_info"]["gender"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"].blank?) 
     user.location = auth["extra"]["raw_info"]["location"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"].blank? && !auth["extra"]["raw_info"]["location"].blank?)   
     user.token = auth["credentials"]["token"] unless auth["credentials"].blank? 


     # highschool data 
     user.highschool_name = auth["extra"]["raw_info"]["education"][0]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["school"].blank?) 
     user.highschool_year = auth["extra"]["raw_info"]["education"][0]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["year"].blank?) 
     # graduate school data 
     user.graduateschool_name = auth["extra"]["raw_info"]["education"][1]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["school"].blank?) 
     user.graduateschool_year = auth["extra"]["raw_info"]["education"][1]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["year"].blank?) 
     user.add_friends 
     user.save! 
     user 
    end 
    end 

    def add_friends 
    facebook.get_connections("me", "friends").each do |hash| 
     self.friends.where(:name => hash['name'], :uid => hash['id']).first_or_create 
    end 
    end 

    private 

    def facebook 
    @facebook ||= Koala::Facebook::API.new(token) 
    end 

end 
+0

Sqlite3不能嵌套事務。 – ergosys

+0

是的,我忘了提及.. – SHUMAcupcake

回答

0

的事情是,當用戶打了創建方法,該方法from_omniauth(AUTH)正在檢查是否用戶存在,如果用戶dosent存在它開始創建用戶並從Facebook提供的auth哈希中獲取所有信息。但是,在保存用戶之前,它調用add_friends方法,但用戶仍然存在錯誤!

所以,你必須先保存用戶,然後調用add_friends方法

而且SQLITE3是不能嵌套事務的!

乾杯!

2
bundle exec rake db:reset 

鍵入上述命令。

+0

請提供一些解釋以及..將幫助... – NREZ