0

ActiveRecord::Base.transaction之后怎么用first_or_create

这是我的模型方法:

  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
4

0 回答 0