一旦产品保存在我的产品控制器中,我正在尝试创建事务日志。出于某种原因,当我尝试保存事务日志时出现错误。产品保存成功。
代码:
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)