当我跑的时候
irb(main):003:0> House.new(name: "A house")
我知道错误了
ActiveRecord::StatementInvalid: Could not find table 'houses'
from /home/overflow012/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/sqlite3_adapter.rb:429:in `table_structure'
...
你可以看到下面我的代码
property.rb
class Property < ApplicationRecord
self.abstract_class = true
end
apartment.rb
class Apartment < Property
end
house.rb
class House < Property
end
db/migrate/20160616022800_create_properties.rb
class CreateProperties < ActiveRecord::Migration[5.0]
def change
create_table :properties do |t|
t.string :name
t.string :detail
t.float :price
t.string :type
t.timestamps
end
end
end
属性表是通过rake db:migrate
创建的。
注意事项:我使用rails 5.0.0.rc1
我做错了什么?
发布于 2016-06-16 17:50:46
我相信您需要从您的self.abstract_class
模型中删除Property
行。
将abstract_class
添加到模型将迫使子类绕过父类Property
的隐含STI表命名。本质上,我们是说Property
不能再被实例化了,并且没有数据库表的支持。
因此,Property
的子类不会查找表名的父类,它们将根据自己的类名查找表。
或者,您可以在您的self.table_name = 'properties'
模型中设置Property
,这应该是可行的。然而,这违背了定义abstract_class
的目的。
https://stackoverflow.com/questions/37871408
复制相似问题