3

当我尝试使用 AL 创建新用户时,我在生产安装中遇到了一个奇怪的错误:

ActiveRecord::StatementInvalid: Mysql::Error: 列 'crypted_pa​​ssword' 不能为 null: INSERT INTOusers

特别奇怪的 b/c 它在我的本地盒子上按预期工作。

在两个盒子上运行 Rails 2.3.2 和 ruby​​ 1.8.7。

user.rb:
class User < ActiveRecord::Base
 before_create :set_username
  acts_as_authentic do |c|
    c.require_password_confirmation = false
    c.login_field = "email"
    c.validates_length_of_password_field_options = {:minimum => 4}  
    c.validate_login_field = false  #don't validate email field with additional validations 
  end
end

这是我的生产控制台的输出:

>> u = User.new
=> #<User id: nil, username: nil, email: nil, crypted_password: nil,
    password_salt: nil, persistence_token: nil, single_access_token: nil,
    perishable_token: nil, login_count: 0, failed_login_count: 0,
    last_request_at: nil, current_login_at: nil, last_login_at: nil,
    current_login_ip: nil, last_login_ip: nil, created_at: nil,
    updated_at: nil, is_admin: 0, first_name: nil, last_name: nil>

>> u.full_name = 'john smith'
=> "john smith"
>> u.password = 'test'
=> "test"
>> u.email = 't...@example.com'

=> "t...@example.com"
>> u.valid?
=> true
>> u.save

ActiveRecord::StatementInvalid: Mysql::Error: Column
'crypted_password' cannot be null: INSERT INTO `users`
(`single_access_token`, `last_request_at`, `created_at`,
`crypted_password`, `perishable_token`, `updated_at`, `username`,
`failed_login_count`, `current_login_ip`, `password_salt`,
`current_login_at`, `is_admin`, `persistence_token`, `login_count`,
`last_name`, `last_login_ip`, `last_login_at`, `email`, `first_name`)
VALUES('B-XSXwhO7hkbtISIOyEq', NULL, '2009-07-31 01:10:44', NULL,
'FK3mYS2Tp5Tzeq5IXE1z', '2009-07-31 01:10:44', 'john', 0, NULL, NULL,
NULL, 0,
'2c76b645f761eb3509353290e93874cecdb68a63caa165812ab1b126d63660757090ecf69995caef9e78f93d070b524e2542b3fec4ee050726088c2a9fdb0c9f',
0, 'smith', NULL, NULL, 't...@example.com', 'john')
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/connection_adapters/abstract_adapter.rb:212:in `log'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/connection_adapters/mysql_adapter.rb:320:in `execute'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/connection_adapters/abstract/database_statements.rb:
259:in `insert_sql'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/connection_adapters/mysql_adapter.rb:330:in `insert_sql'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/connection_adapters/abstract/database_statements.rb:
44:in `insert_without_query_dirty'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/connection_adapters/abstract/query_cache.rb:18:in
`insert'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/base.rb:2902:in `create_without_timestamps'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/timestamp.rb:29:in `create_without_callbacks'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/callbacks.rb:266:in `create'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/base.rb:2868:in `create_or_update_without_callbacks'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/callbacks.rb:250:in `create_or_update'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/base.rb:2539:in `save_without_validation'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/validations.rb:1009:in `save_without_dirty'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/dirty.rb:79:in `save_without_transactions'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/transactions.rb:229:in `send'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/transactions.rb:229:in
`with_transaction_returning_status'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/connection_adapters/abstract/database_statements.rb:
136:in `transaction'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/transactions.rb:182:in `transaction'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/transactions.rb:228:in
`with_transaction_returning_status'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/transactions.rb:196:in `save'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/transactions.rb:208:in `rollback_active_record_state!'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/
active_record/transactions.rb:196:in `save'

不知道为什么会发生这种情况,尤其是为什么这可以节省开发人员而不是生产人员的新用户。非常感谢任何帮助,谢谢!

编辑:使用 Apache & Passenger 2.2.4

4

5 回答 5

8

act_as_authentic 混合了 password 和 password= 方法。password= 方法获取您的纯文本密码,对其进行加盐和加密。

也许你已经在你的用户模型中重新定义了 password= 方法,比如 attr_accessor :password?

于 2009-10-14T13:37:02.967 回答
4

当您覆盖这样的访问器时,Authlogic 会用 null 填充 crypted_pa​​ssword 和 password_salt

attr_accessor :password, :password_confirmation

这些参数应该只在 attr_accessible

于 2010-03-11T10:35:15.473 回答
0

在我的项目副本上出现相同的错误,但是另一个开发人员没有问题。

试试这个:

http://railsforum.com/viewtopic.php?id=7414

于 2009-08-01T16:46:20.813 回答
0

将此添加到您的类映射

ClassMappings.register(

  :methods => ["password"]

  )
于 2010-11-16T16:06:36.457 回答
0

您是否在生产/开发系统中安装了相同的 gem 版本的 authlogic?

于 2009-07-31T13:11:05.747 回答