【发布时间】: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_id 和 follower_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