Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order objects by attributes?

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 ?

like image 547
Dev R Avatar asked Oct 30 '22 12:10

Dev R


1 Answers

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']
}
like image 149
Antonio Bardazzi Avatar answered Dec 30 '22 18:12

Antonio Bardazzi