5

I am facing this issue for some time now, please look into it .

object.inspect gives me this output

<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: nil, updated_at: nil, inventory_item_id: 758, costing_wastage: 0.0, pick_from_order_sheet: false>

raise object.to_yaml gives this output

-- !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:
      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: {}
    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

I want to order by created_at value which I am getting when doing raise object.to_yaml how can this be done ?

Dev R
  • 1,892
  • 1
  • 25
  • 38
  • use `RawMaterial.order(:created_at)` Though you can only use this once your object is persisted to the database (in your yaml output it shows new_record: true, which means that it is not persisted and that's why created_at is currently nil) – Kkulikovskis Dec 27 '15 at 10:14
  • I want to use the value which I am getting when I raise the object.to_yaml , there is a created_at value which is not null, can I use it ? – Dev R Dec 27 '15 at 12:41
  • is there are reason why you don't want to save the object to the database? Another version is to make a custom `initialize` method in your model and assign an attribute with the value `Time.now` if you are interested in the time, when the actual object was initiated. Using the attribute you want seems extremely hackish – Kkulikovskis Dec 27 '15 at 13:46
  • @Kkulikovskis I am copying the object1 (doing deep cloning), this goes to the new action of controller hence the new view, the fields are pre-filled with the object1 values which we are copying, but it is not in the order in which it is of object1, hence we want to order it based on the object1 created_at – Dev R Dec 27 '15 at 14:50
  • Oh. Ok, then I guess I can't help, because I am not yet familiar with deep cloning. :) – Kkulikovskis Dec 27 '15 at 14:59

1 Answers1

2

If I understand correctly you have a list of objects of type RawMaterial. You can sort the list using the object field ['attributes']['attributes']['values']['created_at'] which is unique for each object and the Enumerable.sort_by method.

enumerable_of_raw_materials.sort_by { |raw_material|
   raw_material['attributes']['attributes']['values']['created_at']
}
Antonio Bardazzi
  • 2,996
  • 1
  • 23
  • 20
  • Hi, My problem is that .dup method does not duplicate created_at value . I get all the other value apart from created_at . This is the correct behavior as per ruby doc . I need the all the duplicated elements in the form be sorted by their created_at value – Dev R Jan 07 '16 at 08:50
  • If you have in some place a logic where you call the `dup` method I think you can also update the attribute `created_at` in the duplicated object setting them to current time. If you need only to keep a sorting inside a form I suggest to use a brand new attribute, like `position` to decouple the job of ordering inside the form and the job of ordering by date. – Antonio Bardazzi Jan 07 '16 at 22:26