1

我已经将一个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

在 Railsconsole (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.

4

3 回答 3

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 |
+----+------------+------------+
|    |            |            |
+----+------------+------------+
于 2012-08-30T09:18:31.053 回答
0

PauaServiceTable和模型是否Lookup适用于单个表?

如果是这样,那么您的PauaServiceTable模型应该有表名,并且Lookup表应该自动获取它。你也可以看看Single Table Inheritance

如果不是,则意味着两个模型用于两个表,为什么Lookup要继承PauaServiceTable?可能您需要在这两个s之间建立关系(has_many等)。has_onemodel

于 2012-08-30T10:17:49.733 回答
0

对于 Rails 3.2.1+,我相信你想要

class PauaServiceTable < ActiveRecord::Base
  self.abstract_class = true
end

ActiveRecord::Inheritance::ClassMethods

于 2014-10-29T20:21:52.740 回答