3

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

1 Answer 1

2

Message.take(2) returns Array. You need ActiveRecord::Relation.

Try Message.limit(2)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.