【问题标题】:Rails model not inheriting from ActiveRecord::BaseRails 模型不继承自 ActiveRecord::Base
【发布时间】:2016-09-26 16:00:30
【问题描述】:

我刚刚从 Rails 迁移中创建了两个表。一个用于ShippingLabel,另一个用于名为Shippo 的模型。迁移成功运行,它在 Postgres 中正确创建了一个表,它甚至看起来像是将 Shippo 识别为一个常量。然而,它并没有像 Shippo 那样从 ActiveRecord::Base 继承。

shippo.rb

class Shippo < ActiveRecord::Base
end

shipping_label.rb

class ShippingLabel < ActiveRecord::Base

  belongs_to :device_purchase

end

当我运行ShippingLabel &lt; ActiveRecord::Base 时,我得到true。当我在控制台中运行Shippo &lt; ActiveRecord::Base 时,我得到nil

【问题讨论】:

  • Postgres 中的表名是shipposshipping_labels
  • 尝试重新加载您的 Rails 控制台。
  • @llya 我试过reload! 并退出了多次,没有运气
  • 会不会是 ActiveRecord 复数逻辑的问题?还是这两个新表的前 5 个字母相同?这是个奇怪的问题。
  • ActiveRecord 只是查看数据库模式并为列添加访问器和内容。它不会修改模型类的继承。我会尝试设置一个minimal verifiable example,因为错误可能只是某个地方的错误,例如您正在涂抹常量Shippo

标签: ruby-on-rails postgresql activerecord


【解决方案1】:

我通常在对象的实例上使用 class.superclass 来确定超类。

在 Rails 控制台中:

shippo = Shippo.new #shippo is an instance of the class Shippo
shippo.class.superclass

【讨论】:

    猜你喜欢
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多