Ruby on rails Rails activerecord事务在第一次\u或\u创建之后

Ruby on rails Rails activerecord事务在第一次\u或\u创建之后,ruby-on-rails,ruby,activerecord,omniauth,omniauth-facebook,Ruby On Rails,Ruby,Activerecord,Omniauth,Omniauth Facebook,如何在第一次创建或创建之后使用ActiveRecord::Base.transaction 以下是我的模型方法: def self.from_omniauth(auth) where(provider: auth.provider, uid: auth.uid).first_or_create do |user| user.provider = auth.provider user.uid = auth.uid user.name = auth.i

如何在第一次创建或创建之后使用
ActiveRecord::Base.transaction

以下是我的模型方法:

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.email = auth.info.email
      user.password = 'password'
      user.password_confirmation = 'password'
      user.crypted_password = 'password'
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end
在我的控制器中:

     def social_login
        @user = User.from_omniauth(env["omniauth.auth"])

        ActiveRecord::Base.transaction do      
          add_sites(@user)  
          @user.mobile = mobile_view?
          @user.addresses.last.email = @user.email
          add_point_to_customer(@user)
          @user.add_mail_subscription_if_doesnt_exist(active_site)
        end
     end

只需移动user.save!将
用户从\u omniauth
移动到
事务
块中