Jason Stokes
Jason Stokes

Reputation: 707

Why is Model Creation not Working

I'm trying to create a transaction log once a product is saved in my product controller. For some reason I'm getting an error when I try to save the transaction log. The product is being saved successfully.

Code:

def create 
  @product = Product.new(params[:product])                                                                                                      

  respond_to do |format|                                                                                                                        
  if @product.save
    ProductTransaction.create(
      :product_id => @product.id,                                                                                                             
      :user_id => current_user.id,                                                                                                            
      :message => "Product created"                                                                                                           
    )                                                                                                                                         

    format.html { 
      redirect_to product_path(@product.id), 
      :flash => { :success => "Product was successfully created." } 
    }                      
    else
      format.html { render action: "new" }                                                                                                      
    end                                                                                                                                         
  end                                                                                                                                           
end              

Error:

PGError: ERROR:  column "product_id" is of type integer but expression is of type character varying at character 117
HINT:  You will need to rewrite or cast the expression.
: INSERT INTO "product_transactions" ("created_at", "message", "product_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"

I don't understand the error above. I've double check my tables and the product_id is an integer. Also, I tried hard coding a number to see if I could just get it to save. That didn't work. I then removed all the parameters from the create function and still I got the same error. I even recreated the table from scratch and same result. ProductTransaction has no validation requirements. What am I doing wrong?

Code (Parameters removed):

def create 
  @product = Product.new(params[:product])                                                                                                      

  respond_to do |format|                                                                                                                        
  if @product.save
    ProductTransaction.create()                                                                                                                                                                                     

    format.html { 
      redirect_to product_path(@product.id), 
      :flash => { :success => "Product was successfully created." } 
    }                      
    else
      format.html { render action: "new" }                                                                                                      
    end                                                                                                                                         
  end                                                                                                                                           
end              

Product Schema:

Product(id:integer, name:string, etc.)

Product Transaction Schema:

ProductTransaction(id:integer, user_id:integer, product_id:integer, message:integer)

Upvotes: 1

Views: 249

Answers (1)

Jason Stokes
Jason Stokes

Reputation: 707

Found my problem. I had to refresh my app for whatever reason.

ran the following and it worked:

touch tmp/restart.txt

Now if I could only get those two hours of my life back. :-)

Upvotes: 4

Related Questions