# Creating a report generator within a mysql database to be read on rails

**URL:** <https://www.ruby-forum.com/t/creating-a-report-generator-within-a-mysql-database-to-be-read-on-rails/189281>\
**Category:** Rails\
**Created:** [May 27, 2010, 11:38pm UTC](https://www.ruby-forum.com/t/creating-a-report-generator-within-a-mysql-database-to-be-read-on-rails/189281 "2010-05-27T23:38:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jraglin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jraglin](https://www.ruby-forum.com/u/jraglin)\
**Post date:** [May 27, 2010, 11:38pm UTC](https://www.ruby-forum.com/t/creating-a-report-generator-within-a-mysql-database-to-be-read-on-rails/189281/1 "2010-05-27T23:38:10Z")

</div>

Hello,

My issue is that I am creating a database in MySQL and needing to  
export the information in automatically generated reports (.csv), into  
a web application written in Rails.

Does anyone have any advice or tutorial links as to how I may do this?  
Or whether Rails or MySQL have a preferably freeware application that  
could make it much easier?

Thanks

---

<div class="post-metadata">

**Author:** ![jraglin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jraglin](https://www.ruby-forum.com/u/jraglin)\
**Post date:** [May 28, 2010, 12:02am UTC](https://www.ruby-forum.com/t/creating-a-report-generator-within-a-mysql-database-to-be-read-on-rails/189281/2 "2010-05-28T00:02:47Z")

</div>

jraglin wrote:

> Hello,
> 
> My issue is that I am creating a database in MySQL and needing to  
> export the information in automatically generated reports (.csv), into  
> a web application written in Rails.

Well, you can generate the CSV files with MySQL’s export feature and  
read them with FasterCSV. But that seems silly: it would be better to  
have the Rails app connect to the DB directly and get the data it needs.

> Does anyone have any advice or tutorial links as to how I may do this?  
> Or whether Rails or MySQL have a preferably freeware application that  
> could make it much easier?
> 
> Thanks

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jraglin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jraglin](https://www.ruby-forum.com/u/jraglin)\
**Post date:** [May 28, 2010, 11:36am UTC](https://www.ruby-forum.com/t/creating-a-report-generator-within-a-mysql-database-to-be-read-on-rails/189281/3 "2010-05-28T11:36:54Z")

</div>

Hi,

I don’t know if i got you right, but it seems that you are trying to  
import your mysql data into csv report?  
Well, the easiest way to do this is using the fastercsv gem.

1. Install gem : sudo gem install fastercsv
2. Add on top of your controller : require ‘fastercsv’
3. Create your action and view in your controller :

#The action

def import\_csv  
result = YourModel.find(:all, :conditions =\> “your\_condition”)  
@outfile = “your\_csv\_report\_name\_” + Time.now.strftime("%m-%d-%Y") +  
“.csv”

csv\_data = FasterCSV.generate do |csv|  
#field names  
csv \<\< [“Field\_name\_1”]  
csv \<\< [“Field\_name\_2”]

```
#Data
result.each do |table|
  csv << [table.field1 table.field2]
end

#Write data in csv format
send_data csv_data,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=#{@outfile}"

```

end  
redirect\_to :action =\> “your\_action”  
end

#The view import\_csv.html.erb  
\<% form\_tag :action =\> “import\_csv” do %\>  
\<%= submit\_tag ‘Print’ %\>  
\<% end %\>

Hope this helps!

Regards,
