我正在尝试将数组保存到Postgresql。听起来很简单。我正在创建一个订单后,一个成功的条纹付款通过。
#stripe stuff (omitted)
Order.new(
:shipping_price => @order_preview.shipping_price,
:grand_total => @amount,
:cart => @cart,
:items => @cart.line_items
)
@order.save #error here我收到了一个错误:
can't cast ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_LineItem to text如果我正确理解这一点,rails就会告诉我,它不能将数组保存到DB的text列中。为什么不行?
这就是我迁徙的样子..。
def change
add_column :orders, :items, :text, array:true
end我很感谢你的帮助..。
谢谢
编辑:现在试着创建一个ID数组.
:items => @cart.line_items.map { |l| l.product.id }这给了我
PG::DatatypeMismatch at /charges
ERROR: column "items" is of type integer[] but expression is of type text at character 249
HINT: You will need to rewrite or cast the expression.另外,在创建过程中引发错误时,我看到了正确的数组。
items:[1, 2]当然,我需要用散列形式的ID来保存数量,但是.我想得从一开始就开始。
发布于 2014-06-12 23:23:39
@cart.line_items不是数组(它是ActiveRecord::Relation),@cart.line_items.to_a不是String数组(它是LineItems数组)。
您必须传递一个String数组,以便它能够存储在text[]列中。因此,要么需要确定适当的字符串,要么将列类型更改为其他类型。
https://stackoverflow.com/questions/24195286
复制相似问题