lundi 26 décembre 2022

multiple operation in array to create and update record in ruby

I have one use case when i am inserting an object and again updating it in another flow into database. First time, I am storing an 'array of object' into an object. Below is the structure of array of object.

request = [{"question"=>"sample question1", "answer"=>"sample answer1"}, {"question"=>"sample question2", "answer"=>"sample answer2"}]

In another flow, I want to update the same object with updated value.

request = [{"question"=>"sample question1", "answer"=>"updated answer1"}, {"question"=>"sample question2", "answer"=>"updated answer2"}, {"question"=>"sample question1", "answer"=>"sample answer3"}]

Now can someone please suggest me the optimal solution to perform this operation in ruby.

Overall structure should be like this: While inserting first time:

details = [#Details _id:3748y92eh2, name:"random name", email:"xyz@test.com", q&a:[[{"question"=>"sample question1", "answer"=>"sample answer1"},{"question"=>"sample question2", "answer"=>"sample answer2"}]]]

After update:

details = [#Details _id:3748y92eh2, name:"random name", email:"xyz@test.com", q&a:[[{"question"=>"sample question1", "answer"=>"updated answer1"},{"question"=>"sample question2", "answer"=>"updated answer2"}, {"question"=>"sample question1", "answer"=>"sample answer3"}]]]

Since I am new to ruby, can someone please suggest here.

Aucun commentaire:

Enregistrer un commentaire