how to arrange the field in excel by using to_csv

Errr... if you want the data sorted, you have to sort it - it's not
going to magically be in the order you want! Throw an ORDER BY clause
in your SQL.

Are you talking about the ordering of the rows or of the columns?


Attributes of an AR object don't have an order, so you need to specify it explicity when you call to_csv. As per the docs for to_csvm you can do this with the :order or :only options


sorry, i couldn't understand clearly, could you please can you explain
little bit clear?

Read the docs for to_csv

Again… read the docs.

:order - Array of columns in explicit order


@foo.to_csv(:order=>[:title:, :first_name, :middle_name, :last_name,:suffix, :company, :job_title, :business, :street])

To be successful with Rails, learn how to not only read but interpret the documentation. All of this code is open source so even if the docs aren’t clear, you should be able to open the source code and see what it’s doing.

A wise developer once said that you shouldn’t use any code unless you understand how it works.