Sai Phanindhra
Sai Phanindhra

Reputation: 59

ActiveRecord::StatementInvalid in Products#index

I am new to rails 4.I used nested attributes for multiple image upload.But i'm having few problems with this

im getting ActiveRecord::StatementInvalid in Products#index

Mysql2::Error: Unknown column 'pictures.product_id' in 'where clause': SELECT pictures. FROM pictures WHERE pictures.product_id = 11* error

My models are as follows

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

My migration file 20151226132302_add_product_id_to_product.rb

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

Even with above migration product_id is not added to pictures model. Can somebody help me with this? It will be helpful if someone can give me nice reference for RAILS 4

Upvotes: 0

Views: 1804

Answers (1)

Richard Peck
Richard Peck

Reputation: 76784

unknown attribute 'product_id'

Suggests you don't have the product_id column in your pictures table.

If you're using has_many / belongs_to, you'll need to set the foreign_key for your belongs_to model in its table:

enter image description here

If you don't have the product_id column in your pictures table, you'll need to use the following:

$ 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

Ref: Add a reference column migration in Rails 4


Another issue you may have is that you're using .build in the new method. I know that new and build have very little difference, but I was under the impression to use new:

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

You've also got an error in your create action:

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

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

Upvotes: 2

Related Questions