Как предотвратить автоматический откат при попытке установить пользователя в качестве администратора

Привет Активный администратор установлен в моем приложении; Я пытаюсь настроить нового пользователя в качестве администратора с помощью консоли rails. Я выбираю конкретного пользователя в моей таблице user. Затем написание

user.admin = true 

тогда

user.save

но потом

  (0.2ms)  BEGIN
   (0.3ms)  ROLLBACK
=> false

У меня автоматический откат... Что я делаю не так? Вот моя модель пользователя

class User < ActiveRecord::Base
  after_create :send_welcome_email


  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  include AlgoliaSearch
  algoliasearch index_name: "user#{ENV['ALGOLIA_SUFFIX']}" do
    attribute :email, :licence_number, :ranking, :first_name, :last_name
    attributesToIndex ['email', 'licence_number', 'ranking','first_name', 'last_name']
  end

  extend Enumerize
    enumerize :genre, in: [:male, :female]
    enumerize :ranking, in: ['NC', '30/5', '30/4', '30/3', '30/2', '30/1', '30', '15/5', '15/4', '15/3', '15/2', '15/1', '15', '5/6', '4/6', '3/6', '2/6', '1/6', '0', '-2/6', '-4/6', '-15', '-30']
   23

  devise :invitable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, :omniauth_providers => [ :facebook ]
  has_many :subscriptions, dependent: :destroy
  has_many :tournaments, dependent: :destroy
  has_attached_file :picture,
    styles: { medium: "300x300>", thumb: "100x100>" }

  validates_attachment_content_type :picture,
    content_type: /\Aimage\/.*\z/

  has_attached_file :licencepicture,
    styles: { medium: "300x300>", thumb: "100x100>" }

  validates_attachment_content_type :licencepicture,
    content_type: /\Aimage\/.*\z/

  has_attached_file :certifmedpicture,
    styles: { medium: "300x300>", thumb: "100x100>" }

  validates_attachment_content_type :certifmedpicture,
    content_type: /\Aimage\/.*\z/

 validates :first_name, presence: { message: 'Veuillez remplir votre prénom' }, on: :update
 validates :last_name, presence: { message: 'Veuillez remplir votre nom' }, on: :update
  validates :iban, format: {
        with: /\A[a-zA-Z]{2}\d{2}\s*(\w{4}\s*){2,7}\w{1,4}\s*\z/,
        message: 'Le format de votre IBAN doit être du type FR70 3000 2005 5000 0015 7845 Z02'
    }, on: :update
  validates :bic, format: {
        with: /([a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?)/,
        message: 'Le format de votre BIC doit être du type AXABFRPP  '
    }, :allow_blank => true, on: :update

  validates :telephone, format:{
        with: /\A(\+33)[1-9]([-. ]?[0-9]{2}){4}\z/,
        message: 'Le format de votre numéro doit être du type +33602385414'
    }, :allow_blank => true, on: :update

  has_many :messages
  has_many :notifications

  def self.find_for_facebook_oauth(auth)

   where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
     user.provider = auth.provider
     user.uid = auth.uid
     user.email = auth.info.email
     user.password = Devise.friendly_token[0,20]
     user.first_name = auth.info.first_name
     user.last_name = auth.info.last_name
     user.picture = auth.info.image
     user.token = auth.credentials.token
     user.token_expiry = Time.at(auth.credentials.expires_at)
   end
 end

  # def initialize
  #   @errors = ActiveModel::Errors.new(self)
  # end


  def age
    now = Time.now.utc.to_date
    now.year - birthdate.year - (birthdate.to_date.change(:year => now.year) > now ? 1 : 0)
  end
  def full_name
    if last_name != nil && first_name.nil?
      "#{last_name.capitalize}"
    elsif first_name != nil && last_name.nil?
      "#{first_name.capitalize}"
    elsif last_name.nil? && first_name.nil?
      ""
    else
      "#{first_name.capitalize} #{last_name.capitalize}"
    end
  end

  def create_mangopay_natural_user_and_wallet
    natural_user = MangoPay::NaturalUser.create(self.mangopay_user_attributes)


    wallet = MangoPay::Wallet.create({
      Owners: [natural_user["Id"]],
      Description: "My first wallet",
      Currency: "EUR",
      })

    kyc_document = MangoPay::KycDocument.create(natural_user["Id"],{Type: "IDENTITY_PROOF", Tag: "Driving Licence"})

    self.mangopay_natural_user_id= natural_user["Id"]
    self.wallet_id = wallet["Id"]
    self.kyc_document_id = kyc_document["Id"]
    self.save
  end

   def mangopay_user_attributes
    {
      'Email' => self.email,
      'FirstName' => self.first_name,
      'LastName' => self.last_name,  # TODO: Change this! Add 2 columns on users table.
      'Birthday' => self.date_of_birth.to_i,  # TODO: Change this! Add 1 column on users table
      'Nationality' => 'FR',  # TODO: change this!
      'CountryOfResidence' => 'FR' # TODO: change this!
    }
  end

  private


  def send_welcome_email
    UserMailer.welcome(self).deliver
  end
end

person David Geismar    schedule 30.05.2015    source источник
comment
проверка правильности user? user.valid? может быть полезно   -  person DevMarwen    schedule 31.05.2015
comment
Это может быть вызвано одним из условий проверки. Попробуйте использовать user.valid?, чтобы увидеть, что не так, как предложил @Marwen.   -  person Rahul Roy    schedule 31.05.2015
comment
действительно похоже, что мой пользователь недействителен как user.valid? возвращает false, но я не знаю, откуда это взялось   -  person David Geismar    schedule 31.05.2015


Ответы (1)


Поскольку это проблема проверки, ознакомьтесь с руководством. Чтобы получить ошибки:

user.errors.full_messages
person DevMarwen    schedule 30.05.2015