ruby-on-rails - 迭代 Rails 对象数组以创建键/值对

标签 ruby-on-rails ruby activerecord

我有一个 Rails 对象 network_hosts,它返回我的应用程序的所有网络主机。现在,我想创建一个 Morris.js donut chart显示 host.os_vendor 故障(Linux、Windows、Apple 等)。

那么,迭代对象、提取操作系统类型、创建 type count 并将其返回到 Morris.js 的 data 属性的最佳方法是什么?靠什么来创建圆环图?

这是 network_hosts 的输出示例:

[#<NetworkHost id: 80, location_id: 14, mac_address: "70:56:81:C1:7C:FD", ip_address: "10.10.10.122", hostname: "Air1", created_at: "2016-04-14 19:30:20", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Unknown", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 10, location_id: 14, mac_address: "C8:E0:EB:3D:2B:B7", ip_address: "10.10.10.129", hostname: "iMac2", created_at: "2014-11-25 23:20:41", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Unknown", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 29, location_id: 14, mac_address: "18:B4:30:15:BC:63", ip_address: "10.10.10.130", hostname: "02AA01AC4414076R", created_at: "2015-02-26 02:24:32", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Nest Labs", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 30, location_id: 14, mac_address: "00:04:20:F0:6E:9F", ip_address: "10.10.10.135", hostname: "HarmonyHub", created_at: "2015-02-26 02:24:32", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Linux", nickname: nil, vendor: "Slim Devices", os: "Linux 2.6.17 - 2.6.36", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 2, location_id: 14, mac_address: "DC:9F:DB:E6:CB:A9", ip_address: "10.10.10.100", hostname: "office", created_at: "2014-11-25 23:20:41", updated_at: "2016-05-03 18:30:36", deleted_at: nil, os_vendor: "Unknown", nickname: "Who 1", vendor: "Ubiquiti Networks", os: "Unknown", ignore_date: "2016-05-30 00:09:00", ignore_flag: false>,
 #<NetworkHost id: 27, location_id: 14, mac_address: "74:54:7D:02:53:8C", ip_address: "10.10.10.101", hostname: "Hostname Unknown", created_at: "2015-02-26 02:24:32", updated_at: "2016-05-03 18:30:36", deleted_at: nil, os_vendor: "Linksys", nickname: nil, vendor: "Unknown", os: "Linksys WET54G wireless bridge", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 76, location_id: 14, mac_address: "D0:63:B4:00:5B:40", ip_address: "10.10.10.106", hostname: "OpenELEC", created_at: "2016-02-19 01:03:24", updated_at: "2016-05-02 17:08:28", deleted_at: nil, os_vendor: "Unknown", nickname: "OpenElec", vendor: "Unknown", os: "Unknown", ignore_date: "2016-06-01 17:07:50", ignore_flag: false>,
 #<NetworkHost id: 11, location_id: 14, mac_address: "84:34:97:1C:42:50", ip_address: "10.10.10.137", hostname: "HP1A4DC4", created_at: "2014-11-25 23:20:41", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "HP", nickname: nil, vendor: "Unknown", os: "HP printer: LaserJet CM1312, Photosmart C510a, or ...", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 14, location_id: 14, mac_address: "E8:8D:28:57:AE:22", ip_address: "10.10.10.144", hostname: "time-capsule", created_at: "2014-11-25 23:20:42", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Apple", nickname: nil, vendor: "Unknown", os: "Apple AirPort Extreme WAP or Time Capsule NAS devi...", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 75, location_id: 14, mac_address: "00:14:BF:E0:A0:05", ip_address: "10.10.10.2", hostname: "Hostname Unknown", created_at: "2016-02-18 22:48:01", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Linux", nickname: "test", vendor: "Cisco-Linksys", os: "DD-WRT v23 - v24-sp2 (Linux 2.4.20 - 2.4.37)", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 4, location_id: 14, mac_address: "DC:9F:DB:E6:CF:C1", ip_address: "10.10.10.104", hostname: "main", created_at: "2014-11-25 23:20:41", updated_at: "2016-05-03 18:30:36", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Ubiquiti Networks", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 28, location_id: 14, mac_address: "18:B4:30:15:BB:76", ip_address: "10.10.10.105", hostname: "02AA01AC451400G4", created_at: "2015-02-26 02:24:32", updated_at: "2016-05-03 18:30:36", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Nest Labs", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 8, location_id: 14, mac_address: "B8:78:2E:05:E7:F0", ip_address: "10.10.10.118", hostname: "Apple-TV-3", created_at: "2014-11-25 23:20:41", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Apple", nickname: nil, vendor: "Unknown", os: "Apple iOS 4.4.2 - 5.0.1 (Darwin 11.0.0)", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 24, location_id: 14, mac_address: "74:31:70:0B:56:75", ip_address: "10.10.10.113", hostname: "Hostname Unknown", created_at: "2015-01-29 19:05:44", updated_at: "2016-04-24 14:20:44", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Arcadyan Technology", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 82, location_id: 14, mac_address: "d2:2a:aa:3e:1e:5a:2d", ip_address: "10.10.10.222", hostname: "Hostname Unknown", created_at: "2016-04-18 17:45:30", updated_at: "2016-04-24 14:20:44", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Unknown", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 81, location_id: 14, mac_address: "10:9d:07:1b:e4:a7:a8", ip_address: "10.10.10.199", hostname: "Hostname Unknown", created_at: "2016-04-18 16:21:07", updated_at: "2016-04-28 20:49:47", deleted_at: nil, os_vendor: "Linux", nickname: "", vendor: "Unknown", os: "Linux 2.6.32 - 2.6.38", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 1, location_id: 14, mac_address: "4C:E6:76:3F:2F:77", ip_address: "10.10.10.1", hostname: "DD-WRT", created_at: "2014-11-25 23:20:41", updated_at: "2016-05-02 13:25:07", deleted_at: nil, os_vendor: "Linux", nickname: "Router", vendor: "Buffalo", os: "DD-WRT v24-sp2 (Linux 2.6.24)", ignore_date: "2016-05-30 00:56:45", ignore_flag: false>,
 #<NetworkHost id: 41, location_id: 14, mac_address: "54:9F:13:2A:29:16", ip_address: "10.10.10.133", hostname: "TravisFrsiPhone", created_at: "2015-03-05 18:00:18", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Unknown", os: "Unknown", ignore_date: nil, ignore_flag: false>]

最佳答案

如果这是一个ActiveRecord::Relation,那么您可以使用数据库为您分组和计数。

network_hosts.group(:os_vendor).count

# returns a hash
{ "linux" => 5, "unkown" => 35, "mac" => 10, etc... }

您链接的库似乎需要一个包含 2 个键的数组,labelvalue 因此您可以map 转换结果把数据变成你想要的形状

network_hosts
  .group(:os_vendor)
  .count
  .map { |os_vendor, count| { "label" => os_vendor, "value" => count } }

# returns an array of hashes
[ { "label" => "linux", "value" => 5 } ... ]

如果数组只是一个普通的 Ruby 数组,那么你需要使用 Ruby 而不是数据库来分组 + 计数

network_hosts
   .group_by(&:os_vendor)
   .map { |os_vendor, group| { "label" => os_vendor, "value" => group.size } }

关于ruby-on-rails - 迭代 Rails 对象数组以创建键/值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37036280/

相关文章:

ruby - Variable Ruby - Launchschool 的练习

php - 使用事件记录或mysql从codeigniter中的多个表构造json

ruby-on-rails - "config/initializers/new_framework_defaults_5_2.rb"是如何工作的?

ruby-on-rails - Rails 5.2.3/Heroku - 为什么不能在开发模式下使用 squlite3 在生产模式下运行 postgres?

ruby-on-rails - 使用 first_or_create 时 nilClass 的未定义方法 '+'

ruby-on-rails - Rubygems、Bundler 和 RVM 之间的关系

ruby - Ubuntu 12.04 中找不到 Killproc 命令错误

ruby - 带有 Amazon RDS 的 VPC 中的 AWS Elastic Beanstalk

ruby-on-rails-3 - Ruby on Rails - 列名称与关系名称相同

ruby-on-rails - 如何在运行时禁用 Rails 关联 counter_cache