如何在Ruby on Rails中将会话值转换为字符串?

时间:2021-12-03 21:44:00

I want to store the cart items in my table but when I store it using the following code:

我想将购物车项目存储在我的表格中,但是当我使用以下代码存储它时:

@order.items=@cart.items OR @order.items = session[:cart]

it stores values like #<....>. I want the item names to be stored. How is it possible? The order is my table with items column. Any help will be appreciated.

它存储像#<....>这样的值。我希望存储项目名称。这怎么可能?订单是我的表项目列。任何帮助将不胜感激。

1 个解决方案

#1


0  

Try

尝试

@cart.items.collect {|cart| cart.value_you_want_to_collect}

@ cart.items.collect {| cart | cart.value_you_want_to_collect}

ActiveRecord returns an association not just the values, so where there are multiple values, you need to collect them individually.

ActiveRecord不仅返回一个关联值,因此在有多个值的情况下,您需要单独收集它们。

If there are multiple items you want to join you can call .join after the final }

如果您想加入多个项目,可以在最终结束后拨打.join

#1


0  

Try

尝试

@cart.items.collect {|cart| cart.value_you_want_to_collect}

@ cart.items.collect {| cart | cart.value_you_want_to_collect}

ActiveRecord returns an association not just the values, so where there are multiple values, you need to collect them individually.

ActiveRecord不仅返回一个关联值,因此在有多个值的情况下,您需要单独收集它们。

If there are multiple items you want to join you can call .join after the final }

如果您想加入多个项目,可以在最终结束后拨打.join