Search code examples
ruby-on-railsnested-attributes

Not able to order nested_form values in a form when creating new object


I am trying to order the nested form values by created_at . Here is the code which I am using :-

 <%= 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 %> 

The form does not shows blank . When I try to <% raise f.object.raw_materials.inspect %>

I get the below mentioned data :-

#<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>, ...]>

What I could gather is the I might be trying to order nested_objects which has not been built yet . But I need to do it in my use case as the order of the object in which they are displayed and stored is important.


Solution

  • According to this answer's comment, you can use sort_by on the collection (order is ActiveRecord and will expect to ping the database):

    <%= f.fields_for :raw_materials, f.object.raw_materials.sort_by(&:created_at), validate: true do |raw_material| %>