0

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

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]

我的代码有什么问题?

4

1 回答 1

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

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

于 2017-03-31T14:20:16.277 回答