Reputation: 4032
I am trying to add records to a row from an active-record model.
The CSV row is:
csv << [ "Question_name", "Question_description" ]
I am having an answer model which contain name attribute. I want to take all the anwer.name from Answer model and append it to the CSV row. so that the CSV row becomes:
csv << [ "Question_name", "Question_description", "ans1", "ans2", "ans3" ]
I tried to fix the issue and got the following code:
answers = Answer.all
answers.each {|a| ab << a.name }
data = ab.inject(" ") { |x,n| x << "#{n} " }
data.gsub!(/\s/,',')
csv << [ "Question_name", "Question_description", "#{data}" ]
But all the answer.name records were appended in one cell. Please help me to add the answer.name records in different columns
Upvotes: 0
Views: 150
Reputation: 4171
I believe you are looking to acheive this?
answers = Answer.all
row = ["Question_name", "Question_description"] + answers.map(&:name)
csv << row
Upvotes: 3