我已将Rails 2.3.14
项目转换为Rails 3.2.2
对于不直接从ActiveRecord::Base
我的模型定义如下:
class PauaServiceTable < ActiveRecord::Base
def self.abstract_class?
true
end
..
..
end
我有很多类来自这个类。例如:
class Lookup < PauaServiceTable
...
end
在Rails console (3.3.2)
中,我可以实例化直接从Activerecord::Base
下降的任何表,但是从PauaServiceTable
继承的任何表都会失败并显示以下堆栈跟踪:
Loading development environment (Rails 3.2.2)
ruby-1.8.7-p334 :001 > l=Lookup.new
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect table name '': SHOW FULL FIELDS FROM ``
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `query'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `execute'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `log'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activesupport-3.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `execute'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:257:in `execute_and_free'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:424:in `columns'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/connection_adapters/schema_cache.rb:12:in `initialize'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `call'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `default'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `[]'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `columns'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:243:in `column_defaults'
from /Users/george/.rvm/gems/ruby-1.8.7-p334@rails322/gems/activerecord-3.2.2/lib/active_record/base.rb:479:in `initialize'
from (irb):1:in `new'
from (irb):1ruby-1.8.7-p334 :002 >
此代码在Rails 2.3.14
中运行良好,但现在在转换为3.2.2
后失败。
答案 0 :(得分:1)
尝试在@abstract_class = true
class PauaServiceTable
---已编辑
由于某种原因,table_name为空,请尝试以下操作:
class Lookup < PauaServiceTable
set_table_name 'lookups'
end
我可以使用以下代码重现您的错误:
class PauaServiceTable < ActiveRecord::Base
def self.abstract_class?
true
end
end
class Lookup < PauaServiceTable
end
但是,以下工作:
class PauaServiceTable < ActiveRecord::Base
@abstract_class = true
end
class Lookup < PauaServiceTable
end
l= Lookup.new
+----+------------+------------+
| id | created_at | updated_at |
+----+------------+------------+
| | | |
+----+------------+------------+
答案 1 :(得分:0)
单个表格的PauaServiceTable
和Lookup
模型是什么?
如果是这样,那么您的PauaServiceTable
模型应该具有表名,Lookup
表应该自动获取它。您还可以查看Single Table Inheritance
。
如果没有,意味着两个模型用于两个表,为什么Lookup
应该继承PauaServiceTable
?您可能需要在这两个has_many
之间建立关系(has_one
,model
等)。
答案 2 :(得分:0)
对于Rails 3.2.1+,我相信你想要
class PauaServiceTable < ActiveRecord::Base
self.abstract_class = true
end