0

How do I split this array and store into the database?

I have three fields in my model called Question_id, Answer_id and Phase_id.

I have a result like:

question_hash_string = "{\"5\":[\"5\",\"0\",\"\"],\"25\":[\"25\",\"1\",\"3\"]}"}

Which looks like {5:[5,0,1], 25:[25,1,3] ... }.

I want to split the array and store the results into the three fields in order Question, Answer and Phase of each set.

In my Batch table, I have three columns: question_id, answer_id and phase_id.

The first value of array[5,0,1], 5 goes to question_id, 0 to answer_id and 1 to phase_id. In the second row 25 to question_id, 1 to answer_id and 3 to phase_id.

5
  • How do you want to store I mean all are in same table?? Commented Jan 15, 2013 at 17:25
  • @checkit Yes, If i have a table called Batch and three columns like Question, answer and phase. I want to store first value to question, second to answer and third to phase of each array list in row by row in the database... Commented Jan 15, 2013 at 17:27
  • is key indicates batch id?? Commented Jan 15, 2013 at 17:29
  • key indicates the question_id.. but the question_id is also inside the array. so if we need, we can take the key otherwise take it from the arraylist. Commented Jan 15, 2013 at 17:30
  • i have answered it. did u check? Commented Jan 15, 2013 at 17:34

2 Answers 2

3

You can do this:

hash_values = JSON.parse(question_hash_string)
hash_values.each do |k,v|
  b = Batch.new
  b.question_id, b.answer_id, b.phase_id = v.collect(&:to_i)
  b.save!
end
Sign up to request clarification or add additional context in comments.

15 Comments

sometimes the answer would come in a string? is it possible to do the same for a string inside the array?
string means, can you give an example?
for example, {5:[5,Name,1], 25[25,1,3] and so on }
Please clarify of Whats the k for in the hash_values.each do?
If you use v instead of v.collect(&:to_i) v looks like ["25", "false", "3"] so if you have all fields as char(30) it will store values as string. Ruby is dynamically typed not your SQL(mysql, sqlite, whatever you using). But your column names are not semantically correct.
|
2

You should be able to parse this with JSON:

json_loaded = JSON.load(question_hash_string)

From there you can emit in any format you might wish, but will need to convert your values to integers:

remapped = Hash[
  json_loaded.collect do |k, a|
    [ k.to_i, a.collect(&:to_i) ]
  end
]
# => {5=>[5, 0, 0], 25=>[25, 1, 3]}

JSON.dump(remapped)
# => {"5":[5,0,0],"25":[25,1,3]}

Since JSON requires string keys, this is very close to what you want. To get it precisely the same you'd have to write a custom emitter.

2 Comments

Thank you ... am a newbie.. so will you tell me how to select the first value of the first array? I don't have an idea of custom emitter...
@Vinay : remapped.first will return the first key value pair ["5", [1, 2, 3]] as an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.