ruby - Rails model relation MissingAttributeError -
i have models in application, , i'm trying save data got error:
activemodel::missingattributeerror (can't write unknown attribute product_id
): app/controllers/admin_controller.rb:27:in `create_product'
i have 3 models
category model
class category < activerecord::base has_many :features has_many :products end
migration:
class createcategories < activerecord::migration def change create_table :categories |t| t.string :name, null: false t.boolean :active, null: false, default: false t.timestamps null: false end end
feature model
class feature < activerecord::base has_and_belongs_to_many :categories has_and_belongs_to_many :products end
migration
class createfeatures < activerecord::migration def change create_table :features |t| t.belongs_to :category, index:true t.string :name, null: false t.timestamps null: false end end
product model
class product < activerecord::base belongs_to :category has_many :features end
migration
class createproducts < activerecord::migration def change create_table :products |t| t.belongs_to :category, index:true t.string :name, null: false t.text :rating, null: false t.timestamps null: false end end
i got error when try save product
activemodel::missingattributeerror (can't write unknown attribute
product_id
): app/controllers/admin_controller.rb:27:in `create_product'
i cannot figure out what's happening
any ideas?
thanks
if @ error, says
activemodel::missingattributeerror (can't write unknown attribute product_id)
so don't have product_id field , looking @ associations , features table migration, you forgot add product_id field in features table.
fix:
to add product_id field in features table need create new migration , migrate db:
rails g migration addproductidtofeatures product_id:integer rake db:migrate
Comments
Post a Comment