Ruby 分割数组。红宝石

Ruby 分割数组。红宝石,ruby,arrays,split,Ruby,Arrays,Split,你能帮我解决一些问题吗? 我从数据库中得到了如下数组 str = Hiring::Tag.all Hiring::Tag Load (0.1ms) SELECT `hiring_tags`.* FROM `hiring_tags` => [#<Hiring::Tag id: 1, name: "tag1", created_at: "2013-12-10 11:44:39", updated_at: "2013-12-10 11:44:39">, #<Hi

你能帮我解决一些问题吗? 我从数据库中得到了如下数组

 str = Hiring::Tag.all
  Hiring::Tag Load (0.1ms)  SELECT `hiring_tags`.* FROM `hiring_tags`
=> [#<Hiring::Tag id: 1, name: "tag1", created_at: "2013-12-10 11:44:39", updated_at: "2013-12-10 11:44:39">,
    #<Hiring::Tag id: 2, name: "tag2", created_at: nil, updated_at: nil>,
    #<Hiring::Tag id: 3, name: "tag3", created_at: nil, updated_at: nil>,
    #<Hiring::Tag id: 4, name: "wtf", created_at: "2013-12-11 07:53:04", updated_at: "2013-12-11 07:53:04">,
    #<Hiring::Tag id: 5, name: "new_tag", created_at: "2013-12-11 10:35:48", updated_at: "2013-12-11 10:35:48">]
请帮帮我

一种可能的解决方案:

Hiring::Tag.all.map {|h| {id: h.id, name: h.name} }
请参阅。

一种可能的解决方案:

Hiring::Tag.all.map {|h| {id: h.id, name: h.name} }

如果使用ActiveRecord 4.0,请参阅。

Hiring::Tag.pluck(:id, :name).map{ |id, name| {id: id, name: name} }

如果您使用ActiveRecord 4.0

Hiring::Tag.pluck(:id, :name).map{ |id, name| {id: id, name: name} }

您可以尝试下面的代码

Hiring::Tag.all.inject({}) { |h, f| h[f.name.to_sym] = f.value; h }


您可以尝试下面的代码

Hiring::Tag.all.inject({}) { |h, f| h[f.name.to_sym] = f.value; h }


已回答[Ruby:将ActiveRecord对象数组分组为哈希][1][1]:已回答[Ruby:将ActiveRecord对象数组分组为哈希][1][1]: