Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 684995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:52:38+00:00 2026-05-14T01:52:38+00:00

Exporting some data from mysql to a csv file using FasterCSV. I’d like the

  • 0

Exporting some data from mysql to a csv file using FasterCSV. I’d like the columns in the outputted CSV to be in the same order as the select statement in my query.

Example:

rows = Data.find(
  :all,
  :select=>'name, age, height, weight'
)

headers = rows[0].attributes.keys
FasterCSV.generate do |csv|
  csv << headers
  rows.each do |r|
    csv << r.attributes.values
  end
end

CSV Output:

height,weight,name,age
74,212,bob,23
70,201,fred,24
.
.
.

I want the CSV columns in the same order as my select statement. Obviously the attributes method is not going to work. Any ideas on the best way to ensure that the columns in my csv file will be in the same order as the select statement? Got a lot of data and performance is an issue. The select statement is not static. I realize I could loop through column names within the rows.each loop but it seems kinda dirty.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:52:39+00:00Added an answer on May 14, 2026 at 1:52 am

    Use the Comma gem:

    class Data < ActiveRecord:Base
    
      comma do
        name
        age
        height
        weight
      end
    
      comma :height_weight do
       name
       age
       height_in_feet
       weight
     end
    
    
    end
    

    Now you can generate the CSV as follows:

    Data.all(:select => 'name, age, height, weight').to_comma
    
    Data.all(:select => 'name, age, height_in_feet, weight').to_comma(:height_weight)
    

    Edit:

    The ActiveRecord finders does not support calculated columns in the resultset, i.e.

    data = Data.first(:select => 'name, age, height/12 as height_in_feet, weight')
    data.height_in_feet # throws error
    

    You can use select_extra_columns gem if you want to include the calculated columns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am programmatically exporting data (using PHP 5.2) into a .csv test file. Example
When exporting some data from MS SQL Server using Python, I found out that
I'm exporting data programatically from Excel to SQL Server 2005 using SqlBulkCopy. It works
We're exporting some data from a website app into an Excel spreadsheet, however, when
In ASP.NET, I am exporting some data to Excel by simply binding a DataSet
I'd like a simple example of exporting a function from a C++ Windows DLL.
I sometimes import data from CSV files that were provided to me, into a
I'm exporting some data I have in a database into Excel. Although the below
Our product has the requirement of exporting its native format (essentially an XML file)
What are best practices for moving/exporting SQL Server Integration Services Packages from a SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.