This is probably a huge oversight on my part, but I have been searching for a fix for about an hour now, with no luck!
I am following the RailsCast tutorial on exporting data to .CSV
The data I am using to populate the CSV is coming from a Ransack query (in the params)
However, the data that is being spit out is a collection of the data objects, not the raw data itself --
Output in the Browser:
#<Order:0x007f820823c738>,#<Order:0x007f82082ad708>,#<Order:0x007f82082ace98>,
#<Order:0x007f82082ac718>
And I am just trying to get those same objects, but just printed out... like in the RailsCasts tutorial (Screenshot)
Here is my Orders_Controller
def index
@search = Order.search(params[:q])
@orders = @search.result
@results = @orders
respond_to do |format|
format.html # index.html.erb
format.csv { render text: @results.to_csv }
end
end
So is there an easy way to convert those data objects into a raw string like in the tutorial?
EDIT
Here is my to_csv
method in my model
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv << column_names
all.each do |order|
csv << order.attributes.values_at(*column_names)
end
end
end