【发布时间】:2015-06-19 03:09:27
【问题描述】:
我的应用程序中有一些模型,我正在尝试保存数据但出现错误:
ActiveModel::MissingAttributeError(无法写入未知属性product_id):
app/controllers/admin_controller.rb:27:in `create_product'
我有 3 个模型
类别模型
class Category < ActiveRecord::Base
has_many :features
has_many :products
end
迁移:
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name, null: false
t.boolean :active, null: false, default: false
t.timestamps null: false
end
end
特征模型
class Feature < ActiveRecord::Base
has_and_belongs_to_many :categories
has_and_belongs_to_many :products
end
迁移
class CreateFeatures < ActiveRecord::Migration
def change
create_table :features do |t|
t.belongs_to :category, index:true
t.string :name, null: false
t.timestamps null: false
end
end
产品型号
class Product < ActiveRecord::Base
belongs_to :category
has_many :features
end
迁移
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.belongs_to :category, index:true
t.string :name, null: false
t.text :rating, null: false
t.timestamps null: false
end
end
当我尝试保存产品时出现此错误
ActiveModel::MissingAttributeError(无法写入未知属性
product_id): app/controllers/admin_controller.rb:27:in `create_product'
我不知道发生了什么
有什么想法吗?
谢谢
【问题讨论】:
-
由于错误来自 admin_controller,能否请您也包含该代码?
-
Feature 是 Category 和 Product 模型的连接吗?
标签: ruby-on-rails ruby ruby-on-rails-4 model ruby-on-rails-3.2