Reputation: 1533
It is partly working. It generates the file but the output is not how I want it.
Controller
@messages = Message.take(2)
respond_to do |format|
format.html
format.csv { send_data @messages.to_csv }
end
Message.rb
def self.to_csv
CSV.generate do |csv|
csv << Message.attribute_names
Message.all.each do |message|
csv << message.attributes.values
end
end
end
I get the CSV file downloaded, it contains the records itself but it does not show the columns and values
#<Message:0x007fca7a028338>,#<Message:0x007fca79a6bf58>
I would expect the Message attributes like:
ID,text
1,hello
2,world
Upvotes: 3
Views: 3426
Reputation: 111
Message.take(2)
returns Array. You need ActiveRecord::Relation.
Try Message.limit(2)
Upvotes: 2