简体   繁体   中英

iterate over an array of hashes to create data in rails

I have an array of hashes that I am trying to seed into a database.

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

I'm only getting null values for both department_id and follower_id . user_id is working.

I have tried using "#{n}" and "#{k}" , to get the key values set to department and follower ids. I've also tried iterating over the array only using .each do |a| and setting department_id: a['department_id'], follower_id a['follower_id']

as seen here: iterate through array of hashes in ruby and here : How do I iterate over an array of hashes and return the values in a single string?

but I'm only still getting null values. How can I get my values into the database?

shoe_array is an array of hashes, so you should iterate over each hash, and access each key-value pair:

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

According the documentation you can create records from an array of hashes:

Following should work (You can use create! as well as 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})})

Change your iteration to

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

An example that can use |n, k| would be either a hash or an array of arrays. If you want to go down that route, you can call values on each hash in the array (assuming that the hash is consistent, meaning department_id always comes first before 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]]

Then you can just use your old code or refactor to

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

Take note though that you are iterating over the array twice and will be less efficient compared to the first one.

UPDATE

Another option is use the array elements as is.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM