我有一個散列數組,我試圖種入數據庫。遍歷一個散列數組來創建數據導軌
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?
,但我只仍然得到空值。我怎樣才能將我的值存入數據庫?
這不是做得比較工作。 department_id爲空,而follower_id則相應地工作。如果我切換他們的職位,那麼follower_id是第一個,那麼follower_id是空的,department_id是它應該是。它看起來像它應該工作,我試圖解決它在我的更多。 – user3456978
@ user3456978刪除不需要它們的逗號(請參閱編輯:) :) –