Abolfazl Mahmoodi
Abolfazl Mahmoodi

Reputation: 390

Mapping hash dynamically

I have an ActiveRecord hash like this

#<ActiveRecord::Relation [#<Tipax::Devices::MobileDevice id: 1, serial_number: "234523", imei: "22", user_id: nil>]>

How can I map this hash to an array dynamically by its columns name(serial_number, imei)

my current code for mapping is the following:

mobile_devices.map do |mobile_device|
  [
    mobile_device.serial_number,
    mobile_device.imei,
  ]
end

but I want to have a method to send an array of columns (%w[serial_number imei]) and an ActiveRecord hash to generate this array from hash.

Upvotes: 1

Views: 122

Answers (1)

Mike Szyndel
Mike Szyndel

Reputation: 10593

Doesn't mobile_devices.pluck(:serial_number, :imei) do what you need?

Upvotes: 4

Related Questions