假设我有一个类似的Activerecord结果:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="img_wrap" id="P">
<img class="imgpreview" id="Pim" src="https://3.bp.blogspot.com/-W__wiaHUjwI/Vt3Grd8df0I/AAAAAAAAA78/7xqUNj8ujtY/s1600/image02.png" alt="your image" />
</div>
<br/>
<div class="img_wrap">
<img class="imgpreview" src="http://i.imgur.com/RRUe0Mo.png" alt="your image" />
</div>
返回order.order_numbers
ActiveRecord_Associations_CollectionProxy
请注意=> [#<OrderNumber id: 53425, Order_number: "01100", Order_number_id: 5391, Order_number_status_id: 1, comment: nil, updated_at: "2016-11-18 16:20:36">,
#<OrderNumber id: 53426, Order_number: "04503", Order_number_id: 5391, Order_number_status_id: 1, comment: nil, updated_at: "2016-11-18 16:19:49">,
#<OrderNumber id: 53427, Order_number: "011004", Order_number_id: 5391, Order_number_status_id: 2, comment: nil, updated_at: "2016-11-18 16:19:49">,
#<OrderNumber id: 53428, Order_number: "004505", Order_number_id: 5391, Order_number_status_id: 1, comment: nil, updated_at: "2016-11-18 16:19:49">,
#<OrderNumber id: 53429, Order_number: "4506", Order_number_id: 5391, Order_number_status_id: 2, comment: nil, updated_at: "2016-11-18 16:19:49">,
#<OrderNumber id: 53430, Order_number: "4507", Order_number_id: 5391, Order_number_status_id: 1, comment: nil, updated_at: "2016-11-18 16:19:49">]
,其中1表示打开,2表示已关闭。
如果我想根据“关闭”状态拆分它并将结果输出到单独的数组中,怎么办呢?我只想将Open订单放入他们自己的数组中,但每次从关闭数字移动到开放数字时都会启动一个新数组。
在上面的例子中,结果将是
Array_1将包含ID 53425和53426
Array_2将包含id53428
Array_3将包含id 53430
所以它会将它分成3个不同的哈希值或数组,这些哈希值或数组只包含打开的订单号,它们都在一个数组中。
Order_number_status_id
我愿意接受有关输出格式的建议。
答案 0 :(得分:1)
为什么不只是.split? Array#Split
order.order_numbers.split{|o| o.Order_number_status_id == 0}
答案 1 :(得分:1)
您可以使用group_by
最适合你的情况
答案 2 :(得分:0)
查看标准方法Enumerable#chunk。
您可以执行以下操作:
order.order_numbers.chunk { |o_num| o_num.Order_number_status_id == 1 }.each{|a, b| p b}
将结果保存在新数组中以删除不必要的部分。