我正在尝试通过created_at对嵌套表单值进行排序。这是我正在使用的代码: -
<%= f.fields_for :raw_materials, f.object.raw_materials.order(:created_at => 'asc'), :validate => true do |raw_material| %>
<%= render :partial => "raw_materials_details", :locals => {:raw_material => raw_material} %>
<% end %>
表单不显示空白。当我尝试<% raise f.object.raw_materials.inspect %>
我得到以下提到的数据: -
#<ActiveRecord::Associations::CollectionProxy [#<RawMaterial id: nil, name: "Jam Button 9 mm Antique Silver", rate: 1.0, raw_material_wastage: 0.0, total_raw_material: 8.0, slug: nil, costing_id: nil, created_at: "2015-06-10 09:12:13", updated_at: nil, inventory_item_id: 758, costing_wastage: 0.0, pick_from_order_sheet: false>, #<RawMaterial id: nil, name: "Leather Board 1.5 MM", rate: 6.0, raw_material_wastage: 10.0, total_raw_material: 0.416666666666667, slug: nil, costing_id: nil, created_at: "2015-06-10 09:12:13", updated_at: nil, inventory_item_id: 834, costing_wastage: 12.0, pick_from_order_sheet: false>, #<RawMaterial id: nil, name: "Suede", rate: 35.0, raw_material_wastage: 40.0, total_raw_material: 2.1875, slug: nil, costing_id: nil, created_at: "2015-06-10 09:12:13", updated_at: nil, inventory_item_id: 1547, costing_wastage: 42.0, pick_from_order_sheet: false>, #<RawMaterial id: nil, name: "COW TUMBLE MARBLE BROWN", rate: 115.0, raw_material_wastage: 30.0, total_raw_material: 13.2052951388889, slug: nil, costing_id: nil, created_at: "2015-06-10 09:12:13", updated_at: nil, inventory_item_id: 139, costing_wastage: 32.0, pick_from_order_sheet: true>, #<RawMaterial id: nil, name: "Lining Allen Cooper Black Silver N (Jaq )", rate: 4.35, raw_material_wastage: 12.0, total_raw_material: 13.1076388888889, slug: nil, costing_id: nil, created_at: "2015-06-10 09:12:13", updated_at: nil, inventory_item_id: 837, costing_wastage: 14.0, pick_from_order_sheet: true>, #<RawMaterial id: nil, name: "Rubberised", rate: 30.0, raw_material_wastage: 10.0, total_raw_material: 0.583333333333333, slug: nil, costing_id: nil, created_at: "2015-06-10 09:12:13", updated_at: nil, inventory_item_id: 1294, costing_wastage: 12.0, pick_from_order_sheet: false>, #<RawMaterial id: nil, name: "Foam Sheet 4mm", rate: 2.0, raw_material_wastage: 10.0, total_raw_material: 2.39583333333333, slug: nil, costing_id: nil, created_at: "2015-06-10 09:12:13", updated_at: nil, inventory_item_id: 621, costing_wastage: 12.0, pick_from_order_sheet: false>, #<RawMaterial id: nil, name: "Runner No. 5 D-Ring Antique", rate: 2.5, raw_material_wastage: 0.0, total_raw_material: 5.0, slug: nil, costing_id: nil, created_at: "2015-06-10 09:12:13", updated_at: nil, inventory_item_id: 1303, costing_wastage: 0.0, pick_from_order_sheet: false>, #<RawMaterial id: nil, name: "Magnet Button Big Antique", rate: 10.0, raw_material_wastage: 0.0, total_raw_material: 1.0, slug: nil, costing_id: nil, created_at: "2015-06-10 09:12:13", updated_at: nil, inventory_item_id: 927, costing_wastage: 0.0, pick_from_order_sheet: false>, #<RawMaterial id: nil, name: "Kari Square 1.5\" Antique Silver ( Pedano )", rate: 15.0, raw_material_wastage: 0.0, total_raw_material: 1.0, slug: nil, costing_id: nil, created_at: "2015-06-10 09:12:13", updated_at: nil, inventory_item_id: 778, costing_wastage: 0.0, pick_from_order_sheet: false>, ...]>
我可以收集的是我可能正在尝试订购尚未构建的nested_objects。但我需要在我的用例中这样做,因为显示和存储它们的对象的顺序很重要。
答案 0 :(得分:0)
根据this answer's comment,您可以对集合使用sort_by
(order
为ActiveRecord
,并期望ping数据库):
<%= f.fields_for :raw_materials, f.object.raw_materials.sort_by(&:created_at), validate: true do |raw_material| %>