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