Ruby on rails &引用;ActiveRecord::语句无效:找不到表;rails中的模型继承

Ruby on rails &引用;ActiveRecord::语句无效:找不到表;rails中的模型继承,ruby-on-rails,activerecord,ruby-on-rails-5,Ruby On Rails,Activerecord,Ruby On Rails 5,当我跑的时候 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_

当我跑的时候

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
class Apartment < Property
end
class House < Property
end
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
类属性
公寓.rb

class Property < ApplicationRecord
    self.abstract_class = true
end
class Apartment < Property
end
class House < Property
end
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
等级公寓
house.rb

class Property < ApplicationRecord
    self.abstract_class = true
end
class Apartment < Property
end
class House < Property
end
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
class House
db/migrate/20160616022800\u创建属性。rb

class Property < ApplicationRecord
    self.abstract_class = true
end
class Apartment < Property
end
class House < Property
end
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
class CreateProperties
属性表是通过rake db:migrate创建的 注意:我使用的是rails 5.0.0.rc1


我做错了什么?

我认为您需要从
属性
模型中删除
self.abstract\u class

向模型中添加
abstract\u class
将强制子类绕过父类
属性的隐含STI表命名。本质上,我们说的是
Property
不能再被实例化,也不能由数据库表支持

因此,
Property
的子类不会在父类中查找表名,它们将根据自己的类名查找表


或者,您可以在
Property
模型中设置
self.table\u name='properties'
,这样应该可以工作。但是,这违背了定义
抽象类的目的

我认为您需要从
属性
模型中删除
self.abstract\u类

向模型中添加
abstract\u class
将强制子类绕过父类
属性的隐含STI表命名。本质上,我们说的是
Property
不能再被实例化,也不能由数据库表支持

因此,
Property
的子类不会在父类中查找表名,它们将根据自己的类名查找表


或者,您可以在
Property
模型中设置
self.table\u name='properties'
,这样应该可以工作。但是,这违背了定义
抽象类的目的

您是否尝试过将
属性
作为抽象类?我不明白为什么,但它是有效的!非常感谢。在域问题中,property是一个抽象概念,您是否尝试过不将
property
作为抽象类?我不明白为什么,但它是有效的!非常感谢。在领域问题中,属性是一个抽象的概念