Consider I have these attributes in table: answers, user_id, question_id
This is what I did:
I stored it with the loop but that's bad practice to trigger database again and again:
temp= params.fetch(:user_answer)
temp["answers"].length.times { |i|
UserAnswer.create!(answers: temp["answers"][i], question_id: temp["question_id"][i], student_id: current_user.id)
}
I have values in my params like this:
"user_answer"=>{"answers"=>["C++ is mother of all the languages", "25"], "question_id"=>["7", "10"], "student_id"=>["12", "12"]}
How can I add multiple records in one query with this params?
You can pass to
createarray of argumentsBut firstly you need to create such array
And than
Note: you need to convert params to hash firstly, something like this