I am new to rails and I would like to export a dictionary (of strings).
At the moment I am using this piece of code
Competency.order(:competencies).map(&:competencies).uniq!.join(", ")
To generate:
Build a cage!, Build tha pizza, Demonstrated ability in flying, Demonstrated ability in pizza baking
I would like to export this to csv, however the problem is that it is one array. I would like to create separate strings (comma separated) and displaying each string on a different row. Currently everything is displayed on one row when exporting this to csv.
Any clue how to do this? I created an array of the data because there is a lot of duplicate data and this piece of code is filtering all the duplicates out.
In my controller:
format.csv { render Competency.order(:competencies).map(&:competencies).uniq!.join(", ") }
I hope anyone could help me, much appreciated.
Edit:
Ideally I would like to fetch all competencies and have an output without duplicates, this is my controller:
def dictionary
@competencies = Competency.all
respond_to do |format|
format.html { render html: positions_dictionary_path }
format.csv { render Competency.order(:competencies).map(&:competencies).uniq!.join(", ") }
format.xls { send_data Competency.order(:competencies).map(&:competencies).uniq!.join(", ") }
end
end
Usually in a view file you would do:
<div>
<% @posts.each do |competency| %>
<%= competency.competencies %>
<% end %>
</div>
This generates a list of all the competencies, however also the duplicates. My solution was to create an array. However this creates problems when exporting to csv (because I need every output on a different line/row. So my question, how can I generate a list of all the “competencies” without duplicates?
Rows are not going to be comma separated. They are probable new line terminated. If you want these on separate rows you should maybe try joining them with new lines instead of commas.