【问题标题】:SQLite3::BusyException errorSQLite3::BusyException 错误
【发布时间】:2012-07-02 10:29:36
【问题描述】:

创建新用户帐户时出现此错误:

ActiveRecord::StatementInvalid in SessionsController#create

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

我正在使用 koala 和 omniauth gem 来验证和获取用户朋友。为什么我不能启动两个事务,为什么会回滚好友迁移?

如何解决这个问题?

这是我的会话控制器:

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

【问题讨论】:

  • Sqlite3 无法嵌套事务。
  • 是的,我忘了提......

标签: ruby-on-rails ruby ruby-on-rails-3 sqlite


【解决方案1】:
bundle exec rake db:reset

输入上面的命令。

【讨论】:

    【解决方案2】:

    问题是,当用户点击 created 方法时,方法 from_omniauth(auth) 正在检查用户是否存在,如果用户不存在,它开始创建用户并从 Facebook 提供的 auth 哈希中获取所有信息。但是在保存用户之前,它调用了add_friends方法,用户不存在,因此错误!

    所以你得先保存用户,然后调用add_friends方法

    而且Sqlite3不能嵌套事务!

    干杯!

    【讨论】:

      猜你喜欢
      • 2010-09-09
      • 2011-08-24
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 2013-11-13
      • 2017-02-07
      • 2015-06-05
      • 2020-12-19
      相关资源
      最近更新 更多