2013-03-06 5 views
1

제품이 내 제품 컨트롤러에 저장되면 트랜잭션 로그를 만들려고합니다. 어떤 이유로 트랜잭션 로그를 저장하려고 할 때 오류가 발생합니다. 제품을 성공적으로 저장하는 중입니다.모델 생성이 작동하지 않는 이유

코드 :

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    

오류 : 나는 위의 오류를 이해하지 못하는

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" 

. 테이블을 두 번 확인했는데 product_id는 정수입니다. 또한 숫자를 하드 코딩하여 저장할지 여부를 확인했습니다. 그것은 작동하지 않았다. 그런 다음 create 함수에서 모든 매개 변수를 제거했지만 여전히 동일한 오류가 발생했습니다. 나는 심지어 처음부터 테이블을 재창조했다. ProductTransaction에는 유효성 검사 요구 사항이 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

코드 (제거 매개 변수) :

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(id:integer, name:string, etc.) 

제품 거래 스키마 :

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

'@ product.id'는 무엇입니까? 추측하기 어렵도록 테이블 스키마를 볼 수 없습니다. –

+0

콜백을 사용하고 있습니까? 아마 당신의 Product 모델에서'before_create'라고할까요? 그렇지 않다면 어딘가에서 기본값을 설정하고 있습니까? 연관을 올바르게 설정했다고 가정 할 때'create.' 대신'@ product.product_transactions.build (...)'를 사용해볼 수도 있습니다. 그러면'product_id'가 설정됩니다. – Noz

+0

콜백을 before_create하지 않습니다. –

답변

4

내 문제를 발견. 어떤 이유로 든 내 앱을 새로 고쳐야했습니다.

는 다음을 실행하며 일 :

touch tmp/restart.txt 

을 지금은 다시 내 삶의 두 시간 얻을 수 있다면. :-)

관련 문제