samedi 26 décembre 2015

Order objects after duplicating/Deep cloning them Rails 4

I have a feature which copies the exiting object . The objects have multiple level of nesting . I am using deep_clone to achieve this .

Here is my controller code :-

@costing = @old_costing.deep_clone :include => [{:style => :images}, {:raw_materials => :costing_items}, :other_cost_fixeds, :other_costs, :exchange_rates ], :use_dictionary => true do |original, kopy|
                kopy.remote_picture_url = original.picture_url if kopy.is_a?(Image)

This redirects the request to a pre filled (with old costing) new view

In view I am doing this

<%= f.fields_for :other_cost_fixeds, f.object.other_cost_fixeds.order(:created_at => 'asc') , :validate => true do |fixed_cost| %>

The whole object disappears after doing this . I think this might be happening because the new object has not been created yet ? but if that is the case then how to order it ?

This is the object detail :-

- !ruby/object:RawMaterial
  raw_attributes:
    costing_id: 
    id: 
    name: Jam Button 9 mm Antique Silver
    rate: '1'
    raw_material_wastage: '0'
    total_raw_material: '8'
    slug: 
    created_at: 
    updated_at: 
    inventory_item_id: '758'
    costing_wastage: '0'
    pick_from_order_sheet: f
  attributes: !ruby/object:ActiveRecord::AttributeSet
    attributes: !ruby/object:ActiveRecord::LazyAttributeHash
      types: &6
        id: &3 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer
          precision: 
          scale: 
          limit: 
          range: !ruby/range
            begin: -2147483648
            end: 2147483648
            excl: true
        name: &2 !ruby/object:ActiveRecord::Type::String
          precision: 
          scale: 
          limit: 255
        rate: &1 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Float
          precision: 
          scale: 
          limit: 
        raw_material_wastage: *1
        total_raw_material: *1
        slug: *2
        costing_id: *3
        created_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
          subtype: &4 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::DateTime
            precision: 
            scale: 
            limit: 
        updated_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
          subtype: *4
        inventory_item_id: *3
        costing_wastage: *1
        pick_from_order_sheet: &5 !ruby/object:ActiveRecord::Type::Boolean
          precision: 
          scale: 
          limit: 
      values:
        id: '70'
        name: Jam Button 9 mm Antique Silver
        rate: '1'
        raw_material_wastage: '0'
        total_raw_material: '8'
        slug: 
        costing_id: '34'
        created_at: '2015-06-10 09:12:13.721016'
        updated_at: '2015-06-10 09:12:14.075739'
        inventory_item_id: '758'
        costing_wastage: '0'
        pick_from_order_sheet: f
      additional_types: &7 {}
      materialized: true
      delegate_hash:
        costing_id: !ruby/object:ActiveRecord::Attribute::FromUser
          name: costing_id
          value_before_type_cast: 
          type: *3
          value: 
        id: !ruby/object:ActiveRecord::Attribute::FromDatabase
          name: id
          value_before_type_cast: 
          type: *3
          value: 
        name: !ruby/object:ActiveRecord::Attribute::FromDatabase
          name: name
          value_before_type_cast: Jam Button 9 mm Antique Silver
          type: *2
          value: Jam Button 9 mm Antique Silver
        rate: !ruby/object:ActiveRecord::Attribute::FromDatabase
          name: rate
          value_before_type_cast: '1'
          type: *1
          value: 1.0
        raw_material_wastage: !ruby/object:ActiveRecord::Attribute::FromDatabase
          name: raw_material_wastage
          value_before_type_cast: '0'
          type: *1
          value: 0.0
        total_raw_material: !ruby/object:ActiveRecord::Attribute::FromDatabase
          name: total_raw_material
          value_before_type_cast: '8'
          type: *1
          value: 8.0
        slug: !ruby/object:ActiveRecord::Attribute::FromDatabase
          name: slug
          value_before_type_cast: 
          type: *2
          value: 
        created_at: !ruby/object:ActiveRecord::Attribute::FromUser
          name: created_at
          value_before_type_cast: 
          type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
            subtype: *4
          value: 
        updated_at: !ruby/object:ActiveRecord::Attribute::FromUser
          name: updated_at
          value_before_type_cast: 
          type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
            subtype: *4
          value: 
        inventory_item_id: !ruby/object:ActiveRecord::Attribute::FromDatabase
          name: inventory_item_id
          value_before_type_cast: '758'
          type: *3
          value: 758
        costing_wastage: !ruby/object:ActiveRecord::Attribute::FromDatabase
          name: costing_wastage
          value_before_type_cast: '0'
          type: *1
          value: 0.0
        pick_from_order_sheet: !ruby/object:ActiveRecord::Attribute::FromDatabase
          name: pick_from_order_sheet
          value_before_type_cast: f
          type: *5
          value: false
  new_record: true

As it can be seen the created_at value is appearing later and the created_at of the new_object is obviously empty, how do I access the created_at value which is appearing ?

Aucun commentaire:

Enregistrer un commentaire