从ActivereCord模型的收集中构建哈希

发布于 2024-10-29 10:57:18 字数 597 浏览 2 评论 0原文

我正在尝试从模型中构建哈希。

这就是我想要构建的哈希类型。

{"United Sates" => "us", "United Kingdom" => "uk" .....}

现在,我尝试了很多方法,我只是四处走动。

这只是我的一些糟糕的尝试。

select = Array.new
countries.each do |country|
  # select.push({country.name => country.code })
  # select[country.name][country.code]
end

h = {}

countries.each do |c|
  # h[] = {c.name => c.code}
  # h[] ||= {} 
  # h[][:name] = c.name
  # h[][:code] = c.code 
  #h[r.grouping_id][:name] = r.name
  # h[r.grouping_id][:description] = r.description
end

请一些建议。

谢谢

I'm trying to build a hash from a Model.

This is the type of hash I want to build.

{"United Sates" => "us", "United Kingdom" => "uk" .....}

I have tried so many ways now I'm just going around in circles.

Here are just some of my poor attempts.

select = Array.new
countries.each do |country|
  # select.push({country.name => country.code })
  # select[country.name][country.code]
end

h = {}

countries.each do |c|
  # h[] = {c.name => c.code}
  # h[] ||= {} 
  # h[][:name] = c.name
  # h[][:code] = c.code 
  #h[r.grouping_id][:name] = r.name
  # h[r.grouping_id][:description] = r.description
end

Please can some advise.

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白云悠悠 2024-11-05 10:57:18

以下是一些简单的替代方案:

# Ruby 2.1+
name_to_code = countries.map{ |c| [c.name,c.code] }.to_h

# Ruby 1.8.7+
name_to_code = Hash[ countries.map{ |c| [c.name,c.code] } ]

# Ruby 1.8.6+
name_to_code = Hash[ *countries.map{ |c| [c.name,c.code] }.flatten ]

# Ruby 1.9+
name_to_code = {}.tap{ |h| countries.each{ |c| h[c.name] = c.code } }

# Ruby 1.9+
name_to_code = countries.to_a.each_with_object({}){ |c,h| h[c.name] = c.code }

由@Addicted 下面的评论提供:

# Ruby 1.8+
name_to_code = countries.inject({}){ |r,c| r.merge c.name=>c.code }

Here are some one-liner alternatives:

# Ruby 2.1+
name_to_code = countries.map{ |c| [c.name,c.code] }.to_h

# Ruby 1.8.7+
name_to_code = Hash[ countries.map{ |c| [c.name,c.code] } ]

# Ruby 1.8.6+
name_to_code = Hash[ *countries.map{ |c| [c.name,c.code] }.flatten ]

# Ruby 1.9+
name_to_code = {}.tap{ |h| countries.each{ |c| h[c.name] = c.code } }

# Ruby 1.9+
name_to_code = countries.to_a.each_with_object({}){ |c,h| h[c.name] = c.code }

Courtesy of @Addicted's comment below:

# Ruby 1.8+
name_to_code = countries.inject({}){ |r,c| r.merge c.name=>c.code }
戈亓 2024-11-05 10:57:18

使用 Rails 4,您可以简单地执行以下操作:

country_codes = Hash[Country.pluck(:name, :code)]

我认为这是最佳选择,因为您不必加载一堆国家对象并迭代它们

Rails 3 上的 pluck 方法不允许多个属性,但您可以执行类似的操作:

 country_codes = Hash[Country.connection.select_rows(Country.select('name, code').to_sql)]

With Rails 4 you could simply do:

country_codes = Hash[Country.pluck(:name, :code)]

Which I think is optimal because you don't have to load a bunch of country objects and iterate through them

The pluck method on Rails 3 does not allow more than one attribute, but you could do something like:

 country_codes = Hash[Country.connection.select_rows(Country.select('name, code').to_sql)]
清旖 2024-11-05 10:57:18

这些天我最喜欢的答案是使用 pluckto_h

countries.pluck(:name, :code).to_h
# => {"United Sates" => "us", "United Kingdom" => "uk" .....}

来反转它们并首先获得代码

countries.pluck(:code, :name).to_h
# => {"us" => "United Sates", "uk" => "United Kingdom" .....}

My favorite Answer these days is to use pluck and to_h

countries.pluck(:name, :code).to_h
# => {"United Sates" => "us", "United Kingdom" => "uk" .....}

to reverse them and have the code first

countries.pluck(:code, :name).to_h
# => {"us" => "United Sates", "uk" => "United Kingdom" .....}
江湖彼岸 2024-11-05 10:57:18

定义国家/地区散列,然后从您的记录中填充它。

countries_hash = {}
countries.each do |c|
  countries_hash[c.name] = c.code
end

Define the countries hash then fill it from your records.

countries_hash = {}
countries.each do |c|
  countries_hash[c.name] = c.code
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文