【发布时间】:2014-06-12 23:10:14
【问题描述】:
我正在尝试将数组保存到 Postgresql DB。听起来很简单。在成功完成条带付款后,我正在创建订单。
#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 会告诉我它无法将数组保存到我的数据库的文本列中。为什么不呢?
这就是我的迁移的样子...
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 的数量,但是……我猜得从头开始。
【问题讨论】:
标签: ruby-on-rails ruby rails-activerecord