为什么设计动力模型的after_create挂钩不起作用?

时间:2013-08-05 18:04:59

标签: ruby-on-rails devise

我正在使用带有rails 4的设计3.0.0。在用户模型中我有:

  after_create do |user|
    user.avatars.create! active: true
  end

我在控制台测试了它(rails c)

u = User.new email: 'sss@sasa.com', password: 'password', password_confirmation: 'password', confirmed_at: Time.now

当我测试u.valid?时,它返回true。但是,如果我做u.save!会有这个例外:

irb(main):004:0> u.save!
   (1.3ms)  BEGIN
  User Exists (0.5ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = 'sss@sasa.com' LIMIT 1
  SQL (0.6ms)  INSERT INTO "users" ("confirmed_at", "created_at", "email", "encrypted_password", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["confirmed_at", Tue, 06 Aug 2013 01:56:24 CST +08:00], ["created_at", Tue, 06 Aug 2013 01:56:33 CST +08:00], ["email", "sss@sasa.com"], ["encrypted_password", "$2a$10$prbSSAErr7fpJcCu/z.G8.PCvgGfn/n4RZfRk1AEiDKx1GNB6r8uq"], ["updated_at", Tue, 06 Aug 2013 01:56:33 CST +08:00]]
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1  [["id", 13]]
   (0.2ms)  ROLLBACK
ActiveRecord::RecordInvalid: translation missing: zh-CN.activerecord.errors.messages.record_invalid
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/validations.rb:57:in `save!'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:275:in `block in save!'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:200:in `transaction'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:275:in `save!'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:37:in `insert_record'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/associations/collection_association.rb:463:in `block (2 levels) in create_record'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/associations/collection_association.rb:367:in `add_to_target'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/associations/collection_association.rb:461:in `block in create_record'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/associations/collection_association.rb:152:in `block in transaction'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:200:in `transaction'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/associations/collection_association.rb:151:in `transaction'
... 14 levels...
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/validations.rb:57:in `save!'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:275:in `block in save!'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:275:in `save!'
    from (irb):4
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/jx/.rvm/gems/ruby-2.0.0-p247@rails-4.0/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
    from bin/rails:4:in `require'

u.errors没有显示错误:

irb(main):006:0> u.errors
=> #<ActiveModel::Errors:0x007fd6ee613178 @base=#<User id: nil, confirmed_at: "2013-08-05 17:56:24", confirmation_sent_at: nil, confirmation_token: nil, email: "sss@sasa.com", encrypted_password: "$2a$10$prbSSAErr7fpJcCu/z.G8.PCvgGfn/n4RZfRk1AEiDKx...", reset_password_sent_at: nil, reset_password_token: nil, remember_created_at: nil, current_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_at: nil, last_sign_in_ip: nil, sign_in_count: 0, created_at: "2013-08-05 17:56:33", updated_at: "2013-08-05 17:56:33">, @messages={}>

after_create回电有什么问题?谢谢!

1 个答案:

答案 0 :(得分:1)

你有一些翻译缺失:zh-CN.activerecord.errors.messages.record_invalid,  只是为了得到公认的答案:D

THX