0

我有简单的模块,并将其纳入到我的模型关系不存在的ActiveSupport ::关注

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] 

什么是错的用我的代码?

+0

您是否生成了输入模型并运行'rake db:migrate'?愚蠢的问题,但我想问问。 – jvillian

+0

你是对的,我没有 – user

+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 

我很想知道,如果语法你使用你的作品。快乐元编程!

+1

ActiveSupport :: Concern的全部概念是删除(或隐藏)这些'self.included(base)'方法并使它们更具可读性:http://api.rubyonrails.org/classes/ActiveSupport/Concern.html –

+0

@SebastianvomMeer - 感谢您的链接!我想我是那些避开疑虑的人之一 - 因此我对句法糖不熟悉。所以,再次感谢你的指针。 – jvillian