产品#index中的ActiveRecord :: StatementInvalid

时间:2015-12-26 13:54:26

标签: ruby-on-rails activerecord

我是rails 4的新手。我使用嵌套属性进行多个图片上传。但是这个问题我没什么问题

我在产品#index

中获得 ActiveRecord :: StatementInvalid

Mysql2 ::错误:'where子句'中的未知列'pictures.product_id':SELECT pictures FROM pictures WHERE picturesproduct_id = 11 * 错误

我的模型如下

class Picture < ActiveRecord::Base
   belongs_to :product
   has_attached_file :image
end

class Product < ActiveRecord::Base
    belongs_to :user
    belongs_to :category
    has_many :comments , dependent: :destroy
    has_many :pictures
    accepts_nested_attributes_for :pictures 
end

products_controller.rb

class ProductsController < ApplicationController
def show
    @product = Product.find(params[:id])
    @comment = @product.comments.build
    @category_id = @product.category_id
    @category1 = Category.find_by(id: @category_id)
end
def new
    @product = current_user.products.build
    @product.pictures.build
end
def create
    @product = current_user.Product.new(product_params)
    @product.save
respond_to do |format|
  if @product.save
    format.html { redirect_to @product, notice: 'Product was successfully created.' }
    format.json { render :show, status: :created, location: @product }
  else
    format.html { render :new }
    format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
  @product = Product.find(params[:id])
end
def product_params
  params.require(:product).permit(:name, :price, :description, :reason, :user_id,:status,:category_id,pictures_attributes: [:image])
end

def correct_user
  @product = current_user.products.find_by(id:  params[:id])
  redirect_to root_url if @product.nil?
end
end

Schema.rb

 ActiveRecord::Schema.define(version: 20151226132302) do
create_table "pictures", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "products", force: :cascade do |t|
t.datetime "created_at",                       null: false
t.datetime "updated_at",                       null: false
t.string   "name",               limit: 255
t.integer  "price",              limit: 4
t.text     "description",        limit: 65535
t.text     "reason",             limit: 65535
t.integer  "user_id",            limit: 4
t.string   "image_file_name",    limit: 255
t.string   "image_content_type", limit: 255
t.integer  "image_file_size",    limit: 4
t.datetime "image_updated_at"
t.string   "status",             limit: 255
t.integer  "category_id",        limit: 4
t.integer  "product_id",         limit: 4
end
end

我的迁移文件 的 20151226132302_add_product_id_to_product.rb

class AddProductIdToPictures < ActiveRecord::Migration
   def change
      add_reference :pictures, :product, index: true
   end
   end

即使有上述迁移,product_id也不会添加到图片模型中。 有人可以帮我这个吗? 如果有人可以为 RAILS 4

提供很好的参考,将会很有帮助

1 个答案:

答案 0 :(得分:2)

  

未知属性'product_id'

建议您在product_id表格中没有pictures列。

如果您使用的是has_many / belongs_to,则需要在其表格中为belongs_to模型设置foreign_key

enter image description here

如果您的product_id表格中没有pictures列,则需要使用以下内容:

$ rails g migration AddProductIdToPictures

# db/migrate/add_product_id_to_pictures_________.rb
class AddProductIdToPictures < ActiveRecord::Migration
   def change
      add_reference :pictures, :product, index: true
   end
end

$ rake db:migrate

参考:Add a reference column migration in Rails 4

您可能遇到的另一个问题是您在新方法中使用.build。我知道newbuild差别很小,但我觉得使用new

def new
   @product = current_user.products.new
   @product.pictures.build
end

您的create行动中也出现了错误:

def create
    #NO - @product = current_user.Product.new(product_params) #-> cannot use constant

    #Should be this:
    @product = current_user.products.new
    @product.save
end