rj487
rj487

Reputation: 4634

Rails deserialize format output

I face a problem, I don't know how to arrange the serialize format in rails.

I have models call MissionSet, QuestionSet, Group

The MissionSet will return like this

I want it to become like this, it's really a challenge to me, because I am not familiar with handling this format.

Upvotes: 0

Views: 42

Answers (1)

PoloniculMov
PoloniculMov

Reputation: 219

Here's something that will get you started:

x = {}

inp.each do |h|
  h['assignments'].each do |k, _|
    x[k] ||= []
    x[k] << h['question_set_id']
  end
end

out = x.map do |key, value|
  {
      group_id: key,
      question_sets: value.map { |v| { id: v} }
  }
end

puts out.inspect

This code will first group your questions by the ids in assignments and then format it as you wanted.

Upvotes: 1

Related Questions