ActiveModel :: UnknownAttributeError (неизвестный атрибут 'product_id' для свойства.): - PullRequest
0 голосов
/ 24 сентября 2018

Так что я борюсь с этой ошибкой.При создании проекта реакции на рельсы.

Выполнено 500 Внутренняя ошибка сервера в 75 мс (ActiveRecord: 6,1 мс) ActiveModel :: UnknownAttributeError (неизвестный атрибут 'product_id' для> Property.):

Когда я запускаю этот контроллер:

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

Я пытался найти вещи здесь и здесь .Но я не получаю где.Ниже моя текущая установка.

Модели:

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 голосов
/ 24 сентября 2018

ActiveModel :: UnknownAttributeError (неизвестный атрибут 'product_id' для Property.)

Он говорит, что в таблице properties нет product_id.Это верно, потому что у вас есть Product_id вместо product_id, также как и ошибка.

Соглашения Rails

По умолчанию имена атрибутов должно быть Snakecase .Вы должны сгенерировать миграцию, которая изменит Product_id на product_id и выполнить миграцию, чтобы исправить ошибку.Вы также должны изменить имена ассоциаций на snakecase.Например,

belongs_to :Property
belongs_to :Product

должно быть

belongs_to :property
belongs_to :product
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...