我发现了问题所在。从 Authlogic 的lib/authlogic/acts_as_authentic/base.rb 看这个 sn-p:
private
def db_setup?
begin
column_names
true
rescue Exception
false
end
end
如果column_names 抛出错误,db_setup? 将返回 false。再看这个函数,同样来自base.rb:
def acts_as_authentic(unsupported_options = nil, &block)
# Stop all configuration if the DB is not set up
raise StandardError.new("You must establish a database connection before using acts_as_authentic") if !db_setup?
raise ArgumentError.new("You are using the old v1.X.X configuration method for Authlogic. Instead of " +
"passing a hash of configuration options to acts_as_authentic, pass a block: acts_as_authentic { |c| c.my_option = my_value }") if !unsupported_options.nil?
yield self if block_given?
acts_as_authentic_modules.each { |mod| include mod }
end
如果 db_setup? 返回 false,Authlogic 将抛出异常,但与 column_names 抛出的异常不同。
我的问题是column_names 抛出了这个异常:
/Users/jason/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1106:in `async_exec': PG::Error: ERROR: relation "users" does not exist (ActiveRecord::StatementInvalid)
LINE 4: WHERE a.attrelid = '"users"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"users"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
这个异常的原因是我的用户表被称为user,而不是users,但是Rails 没有正确地接受我的pluralize_table_names 设置。一旦我解决了我的pluralize_table_names 问题(显然这个设置的工作方式在 Rails 3.1 中已经改变),我的 Authlogic 问题就消失了。
所以如果你遇到这个问题,你可能想试试这个:
- 将 Authlogic 存储库克隆到您的开发机器上的某个位置
- 更改您的 Gemfile 以使用本地版本的 Authlogic (
'authlogic', :path => '/path/to/authlogic')
- 在
begin/rescue/end 子句之外添加对db_setup? 的column_names 调用
- 看看你是否像我一样遇到了不同的、可能更准确、信息更丰富的错误