我在遍历一个集合属性以获取值时遇到了问题。
这是我的代理。
<ActiveRecord::Associations::CollectionProxy [#<Product id: 12, name: "test", cost_in_cents: 4400, created_at: "2014-10-31 17:18:58", updated_at: "2014-10-31 17:18:58", subscription: nil, product_type: "One Time">, #<Product id: 14, name: "aggg", cost_in_cents: 1400, created_at: "2014-10-31 17:28:19", updated_at: "2014-10-31 17:28:19", subscription: nil, product_type: "One Time">]>
我希望能够像这样循环遍历代理中的所有对象。
my_collection_proxy.each do |c| c.name end
但是似乎这并不起作用。我如何获取代理中每个名称的值?
输出:
@order.products.each do |a| a.name end
=> [#<Product id: 12, store_front_id: 8, name: "test", cost_in_cents: 4400, created_at: "2014-10-31 17:18:58", updated_at: "2014-10-31 17:18:58", subscription: nil, product_type: "One Time">, #<Product id: 14, store_front_id: 8, name: "aggg", cost_in_cents: 1400, created_at: "2014-10-31 17:28:19", updated_at: "2014-10-31 17:28:19", subscription: nil, product_type: "One Time">]
我希望我的输出看起来像这样:
=> test
=> aggg
@order.products.each do |a| a.name end
的哪里了? - Surya@orders.products{|prod| puts prod.each(&:name)}
是什么意思? - engineersmnky