Reputation:
I am getting this error when a new user account is getting created:
ActiveRecord::StatementInvalid in SessionsController#create
SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction
I am using koala and omniauth gem to autenticate and fetch users friends. Why cant I start two transactions, why does it rollback the friends migration?
How does one fix this?
This is my 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
This is my user model:
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
Upvotes: 1
Views: 1387
Reputation:
The thing is, when the user hit the created method the method from_omniauth(auth) is checking if a user exists, if the user dosent exist its starting creating the user and grabbing all information from auth hash provided from Facebook. But before saving the user it calls the add_friends method, the user dosent exsist yet therefor the error!
So you have to save the user first and then call the add_friends method
And Sqlite3 is incapable of nested transactions!
cheers!
Upvotes: 1