【问题标题】:relation does not exist ActiveSupport::Concern关系不存在 ActiveSupport::Concern
【发布时间】:2017-03-31 13:58:41
【问题描述】:

我有一个简单的模块,它包含在我的模型中

module Inputable
  extend ActiveSupport::Concern

  included do
    has_many :inputs, as: :inputable, dependent: :destroy
  end
end

 class Product < ActiveRecord::Base
    include Inputable
end

但是当我尝试拨打Product.first.inputs 时出现错误

    PG::UndefinedTable: ERROR:  relation "inputs" does not exist
LINE 5: WHERE a.attrelid = '"inputs"'::regclass                                     
: SELECT a.attname, format_type(a.atttypid, a.atttypmod)

Product.reflect_on_all_associations.map { |assoc| assoc.name}
=>[:inputs]

我的代码有什么问题?

【问题讨论】:

  • 您是否生成了输入模型并运行rake db:migrate?愚蠢的问题,但我想我会问。
  • 你说得对,我没有
  • 你能告诉我们你的输入迁移的代码和输入模型吗?

标签: ruby-on-rails activerecord activesupport activesupport-concern


【解决方案1】:

确保您已生成 Input 模型并运行迁移。

另外,当我使用 included 钩子时,它看起来更像这样:

module Inputable
  extend ActiveSupport::Concern

  self.included(base)
    base.class_eval do 
      has_many :inputs, as: :inputable, dependent: :destroy
    end
  end

end

我很想知道您使用的语法是否适合您。元编程快乐!

【讨论】:

  • ActiveSupport::Concern 的整个想法是删除(或隐藏)这些self.included(base) 方法并使它们更具可读性:api.rubyonrails.org/classes/ActiveSupport/Concern.html
  • @SebastianvomMeer - 谢谢你的链接!我想我是那些避开担忧的人之一——因此我不熟悉语法糖。所以,再次感谢您的指点。
猜你喜欢
  • 1970-01-01
  • 2012-09-14
  • 2015-12-29
  • 1970-01-01
  • 2013-02-01
  • 2013-05-26
  • 2014-07-04
  • 1970-01-01
  • 2020-09-21
相关资源
最近更新 更多