2016-12-28 105 views
0

我有一個散列數組,我試圖種入數據庫。遍歷一個散列數組來創建數據導軌

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?

,但我只仍然得到空值。我怎樣才能將我的值存入數據庫?

回答

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 
+1

這不是做得比較工作。 department_id爲空,而follower_id則相應地工作。如果我切換他們的職位,那麼follower_id是第一個,那麼follower_id是空的,department_id是它應該是。它看起來像它應該工作,我試圖解決它在我的更多。 – user3456978

+1

@ user3456978刪除不需要它們的逗號(請參閱編輯:) :) –

1

您的迭代更改爲

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 

請注意,雖然您正在遍歷數組兩次,並且與第一次相比效率較低。

UPDATE

另一種選擇是使用數組元素是。

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

創建一個數組數組也是一個有效的解決方案。因此,在shoe_array.map中,我們將調用數組中的每個散列一次,並使用&:values來抽取department_id和follower_id的散列值到數組中。所以我們現在有一個哈希值的數組。然後我們可以用我們的兩個值遍歷數組的數組。感謝分享! – user3456978

+1

添加了更新。剛剛收到通知,無論出於何種原因我都被拒絕了。 – jvnill

+0

誰知道爲什麼。更新版本看起來像是最好的解決方案。它簡潔明瞭,充分利用了我們以前的術語。 – 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})}) 
+0

非常酷。我不知道我們可以直接使用散列或散列數組創建,儘管另一個答案與.new相同。我們也可以考慮用Relationship.create!(shoe_array.map(&:merge!({user_id:1}))稍微縮短它 – user3456978