2017-03-14 16 views
0

したがって、私はプロジェクトの一部の名前空間を持っています。モデルに名前空間を使用するときのActiveModel :: MissingAttributeError

class Namespace::Product < ActiveRecord::Base 
    belongs_to: :namespace_category, class_name: 'Namespace::Category' 
    ... 
end 

class Namespace::Category < ActiveRecord::Base 
    has_many :products, :class_name => 'Namespace::Product' 
    ... 
end 

製品の移行だから

create_table :namespace_products do |t| 
    t.belongs_to :namespace_category, index: true 
    ... 
end 

のように見える私はこの

p = Namespace::Product.create(some_params) 
c = Namespace::Category.find(id) 
c.products << p 

を行うとき、それはしかし、私は属性

を持って、 ActiveModel::MissingAttributeError: can't write unknown attribute 'category_id'を言って、私にエラーをスローします
t.integer "namespace_category_id" 

はマイグレーションによって作成されたschema.rbにあります。

+0

Shouldn」 t 'has_many:products'は' has_many:namespace_products'に変更されますか? – 31piy

+0

@ 31piyあなたがクラスが指定されている場合、それはどんな方法でも呼び出すことができます – GFalls

+0

'belongs_to :: category、class_name: 'Namespace :: Category''を実際に使用している可能性はありますか?その場合、 'belongs_to :: category、class_name: 'Namespace :: Category'、foreign_key: 'namespace_category_id''を使用する必要があります。 – max

答えて

0

オーケーNamespace::Categoryhas_many :products関連を定義している間、私はちょうどforeign_keyを指定する必要がありました、マックス

0

のおかげで、私はちょうど団体からの名前空間を省略します:

class Namespace::Product < ActiveRecord::Base 
    belongs_to :category, class_name: 'Namespace::Category' 
    ... 
end 

class Namespace::Category < ActiveRecord::Base 
    has_many :products, class_name: 'Namespace::Product' 
    ... 
end 

class FixColumnName < ActiveRecord::Migration 
    def change 
    rename_column :namespace_products, :namespace_category_id, :category_id 
    end 
end 
+0

あなたはコメントしたとおりに外部キーを指定しただけです。ありがとうございました! – GFalls

関連する問題