【问题标题】:ActiveModel::UnknownAttributeError (unknown attribute 'product_id' for Property.):ActiveModel::UnknownAttributeError(Property 的未知属性“product_id”。):
【发布时间】:2018-09-25 01:39:39
【问题描述】:

所以我正在努力解决这个错误。在构建 react on rails 项目时。

在 75 毫秒内完成了 500 个内部服务器错误(ActiveRecord:6.1 毫秒) ActiveModel::UnknownAttributeError(>Property 的未知属性“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

感谢您能给我的任何帮助!

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    ActiveModel::UnknownAttributeError(未知属性 'product_id' 属性。)

    它说properties 表中没有product_id。这是真的,因为你有Product_id 而不是product_id,所以错误也是。

    Rails 约定

    默认情况下,属性名称应该是snakecase。您应该生成一个迁移,将Product_id 更改为product_id 并迁移以修复错误。您还应该将关联名称更改为蛇形。比如

    belongs_to :Property
    belongs_to :Product
    

    应该是

    belongs_to :property
    belongs_to :product
    

    【讨论】:

    • 你能给我一个例子来说明如何做到这一点吗? >您应该生成一个迁移,它将 Product_id 更改为 product_id
    猜你喜欢
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多