关系不存在ActiveSupport ::关注

时间:2017-03-31 13:58:41

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

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

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]

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

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

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

module Inputable
  extend ActiveSupport::Concern

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

end

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