ActiveModel :: UnknownAttributeError(属性的未知属性“ product_id”。):

时间:2018-09-24 17:30:59

标签: ruby-on-rails

所以我正在为这个错误而苦苦挣扎。在构建React on Rails项目时。

  

在75毫秒内完成500个内部服务器错误(ActiveRecord:6.1毫秒)   ActiveModel :: UnknownAttributeError(>属性的未知属性“ product_id”。)

当我运行此控制器时:

class SendDataController < ApplicationController
    protect_from_forgery with: :null_session

    def save
        product = Product.create(name:params[:name], upc:params[:upc].to_i, available_on:params[:availableon])

        property = product.Properties.build(name:params[:properties][0][:name])
        property.save    
    end    
end

我已尝试找到herehere的东西。但是我什么也没有。以下是我当前的设置。

型号:

class ProductProperty < ApplicationRecord
  belongs_to :Property
  belongs_to :Product
end

class Product < ApplicationRecord
  has_many :Properties
  has_many :ProductProperties
end

class Property < ApplicationRecord
  belongs_to :Product
  has_one :ProductProperty
end

迁移:

class CreateProducts < ActiveRecord::Migration[5.2]
  def change
    create_table :products do |t|
      t.string :name
      t.string :upc
      t.datetime :available_on

      t.timestamps
    end
  end
end

class CreateProperties < ActiveRecord::Migration[5.2]
  def change
    create_table :properties do |t|
      t.string :name

      t.timestamps
    end
  end
end

class CreateProductProperties < ActiveRecord::Migration[5.2]
  def change
    create_table :product_properties do |t|
      t.string :value

      t.timestamps
    end
  end
end

class AddProductRefToProperties < ActiveRecord::Migration[5.2]
  def change
    add_reference :properties, :Product, foreign_key: true
  end
end

class AddProductRefToProductProperties < ActiveRecord::Migration[5.2]
  def change
    add_reference :product_properties, :Product, foreign_key: true
  end
end

class AddPropertiesRefToProductProperties < ActiveRecord::Migration[5.2]
  def change
    add_reference :product_properties, :Property, foreign_key: true
  end
end

模式:

ActiveRecord::Schema.define(version: 2018_09_24_163027) do

  create_table "product_properties", force: :cascade do |t|
    t.string "value"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "Product_id"
    t.integer "Property_id"
    t.index ["Product_id"], name: "index_product_properties_on_Product_id"
    t.index ["Property_id"], name: "index_product_properties_on_Property_id"
  end

  create_table "products", force: :cascade do |t|
    t.string "name"
    t.string "upc"
    t.datetime "available_on"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "properties", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "Product_id"
    t.index ["Product_id"], name: "index_properties_on_Product_id"
  end

end

感谢您能给我任何帮助!

1 个答案:

答案 0 :(得分:1)

  

ActiveModel :: UnknownAttributeError(以下情况的未知属性“ product_id”:   财产。)

它表示product_id表中没有properties。没错,因为您有Product_id而不是product_id,所以错误也是如此。

轨道惯例

默认情况下,属性名称应为小写字母。您应该生成一个迁移,该迁移会将Product_id更改为product_id并进行迁移以修复错误。您还应该将关联名称也更改为蛇形。例如

belongs_to :Property
belongs_to :Product

应该是

belongs_to :property
belongs_to :product