【问题标题】:iterate over an array of hashes to create data in rails遍历哈希数组以在 rails 中创建数据
【发布时间】:2017-05-12 07:47:04
【问题描述】:

我有一个哈希数组,我正在尝试将其植入数据库。

shoe_array = [{:department_id=>8, :follower_id=>31}, {:department_id=>9, :follower_id=>41}, {:department_id=>4, :follower_id=>49}, {:department_id=>2, :follower_id=>58}, {:department_id=>5, :follower_id=>36}, {:department_id=>9, :follower_id=>63}, {:department_id=>2, :follower_id=>52}, {:department_id=>23, :follower_id=>26}, {:department_id=>5, :follower_id=>52}, {:department_id=>6, :follower_id=>30}]


shoe_array.each do |n, k|
  department_id = n,
  follower_id = k,
  user_id = 1

  Relationship.create!(department_id: department_id,
                       follower_id: follower_id,
                       user_id: user_id)
end

我只得到了 department_idfollower_id 的空值。 user_id 正在工作。

我尝试使用"#{n}""#{k}" 来获取设置为部门和关注者ID 的键值。我也尝试过仅使用 .each do |a| 并设置 department_id: a['department_id'], follower_id a['follower_id'] 来迭代数组

如此处所示:iterate through array of hashes in ruby 和此处:How do I iterate over an array of hashes and return the values in a single string?

但我仍然得到空值。如何将我的值输入数据库?

【问题讨论】:

    标签: ruby-on-rails arrays ruby hash


    【解决方案1】:

    shoe_array 是一个哈希数组,因此您应该遍历每个哈希,并访问每个键值对:

    shoe_array.each do |hash|
      department_id = hash[:department_id]
      follower_id   = hash[:follower_id]
      user_id       = 1
    
      Relationship.create!(
        department_id: department_id,
        follower_id:   follower_id,
        user_id:       user_id
      )
    end
    

    【讨论】:

    • 这不太行。 department_id 为空,而 follower_id 相应地工作。如果我改变他们的位置,那么 follower_id 是第一位的,那么 follower_id 是 null 并且 department_id 是它应该是什么。它看起来应该可以工作,我正在尝试更多地对其进行故障排除。
    • @user3456978 删除不需要的逗号(见编辑):)
    【解决方案2】:

    根据文档,您可以 create 从哈希数组中记录:

    以下应该可以工作(您可以使用create! 以及create

    shoe_array = [{:department_id=>8, :follower_id=>31}, {:department_id=>9, :follower_id=>41}, {:department_id=>4, :follower_id=>49}, {:department_id=>2, :follower_id=>58}, {:department_id=>5, :follower_id=>36}, {:department_id=>9, :follower_id=>63}, {:department_id=>2, :follower_id=>52}, {:department_id=>23, :follower_id=>26}, {:department_id=>5, :follower_id=>52}, {:department_id=>6, :follower_id=>30}]
    
    Relationship.create!(shoe_array.map{|arr| arr.merge!({user_id: 1})})
    

    【讨论】:

    • 非常酷。我不知道我们可以使用散列或散列数组直接 .create,尽管另一个答案与 .new 相同。我们也可以考虑使用 Relationship.create!(shoe_array.map(&:merge!({user_id: 1})) 稍微缩短它
    【解决方案3】:

    将迭代更改为

    shoe_array.each do |shoe|
      department_id = shoe[:department_id]
      follower_id = shoe[:follower_id]
    

    可以使用|n, k| 的示例可以是散列或数组数组。如果你想走这条路,你可以在数组中的每个散列上调用values(假设散列是一致的,这意味着department_id总是在follower_id之前出现)

    ids = shoe_array.map(&:values) # [[8, 31], [9, 41], [4, 49], [2, 58], [5, 36], [9, 63], [2, 52], [23, 26], [5, 52], [6, 30]]
    

    然后你可以使用你的旧代码或重构来

    ids.each do |department_id, follower_id|
      Relationship.create!(
        department_id: department_id,
        follower_id: follower_id,
        user_id: 1
      )
    end
    

    请注意,虽然您对数组进行了两次迭代,但与第一次相比效率会降低。

    更新

    另一种选择是按原样使用数组元素。

    shoe_array.each do |attributes|
      relationship = Relationship.new(attributes)
      relationship.user_id = 1
      relationship.save!
    end
    

    【讨论】:

    • 创建一个数组数组也是一个有效的解决方案。因此,在 shoe_array.map 中,我们将从数组中调用每个散列一次,在 &:values 中,我们将 Department_id 和 follower_id 的散列值提取到一个数组中。所以我们现在有一个哈希值的数组。然后我们可以用我们的两个值遍历数组数组。感谢分享!
    • 添加了更新。刚收到一条通知,说我因某种原因被否决了。
    • 谁知道为什么。更新的版本看起来是最好的解决方案。它简洁明了,并利用了我们以前的命名法。
    猜你喜欢
    • 2018-09-07
    • 1970-01-01
    • 2016-09-03
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 2019-01-13
    相关资源
    最近更新 更多