<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Good/Bad/Tech &#187; Databases</title>
	<atom:link href="http://goodbadtech.com/category/databases/feed/" rel="self" type="application/rss+xml" />
	<link>http://goodbadtech.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Fri, 15 Jul 2011 18:33:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Certificate drop down menu empty when trying to select SSL certificate to enable SQL Server 2008 client encryption</title>
		<link>http://goodbadtech.com/2011/07/15/sql-encryption/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://goodbadtech.com/2011/07/15/sql-encryption/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 18:28:18 +0000</pubDate>
		<dc:creator>gbt</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://goodbadtech.com/?p=237</guid>
		<description><![CDATA[I recently was trying to enable SSL encryption on my SQL  2008 R2 server to allow a remote client web site application to connect securely.  I started searching the web for blogs, forums, and technet articles that would explain how to do this since I&#8217;ve never done it.  This blog post will not cover how [...]]]></description>
			<content:encoded><![CDATA[<p>I recently was trying to enable SSL encryption on my SQL  2008 R2 server to allow a remote client web site application to connect securely.  I started searching the web for blogs, forums, and technet articles that would explain how to do this since I&#8217;ve never done it.  This blog post will not cover how to set up this configuration in general, Microsoft (<a href="http://technet.microsoft.com/en-us/library/ms191192.aspx" target="_blank">http://technet.microsoft.com/en-us/library/ms191192.aspx</a>) and others have good documentation on this.  The scenario I was having was that even after I had created a certificate, the drop down menu within the SQL Server Configuration Manager for the protocols properties was empty.  I believed I had a couple things working against me that maybe are affecting you as well if you&#8217;re reading this.</p>
<ol>
<li>My SQL server was in a domain that had the Certification Authority role installed, but only partially configured, and the web services portion of the role was not installed.  This limited my ability to create the proper certificate in the first place.</li>
<li>I am not using a SQL cluster, and many of the blog posts and forum threads I read were focused on resolving the empty drop down menu in a cluster environment.</li>
</ol>
<p>For me, this is what worked:</p>
<h3>1. Create the certificate request on the SQL server</h3>
<ul>
<li>Open the MMC console and add the Certificates snap-in for the Local Computer (read the MS technet article above on this for more background)</li>
<li>Right-click the Personal folder and select All Tasks -&gt; Advanced Operations -&gt; Create Custom Request, then click next on the first screen of the enrollment wizard</li>
<li>Select &#8220;Proceed without enrollment policy&#8221; under the custom request section and click Next</li>
<li>Select &#8220;No Template&#8221; Legacy Key under the template drop down, leave other values as default and click Next</li>
<li>On the next screen, click the little down arrow Details button to expand an additional properties window, then click Properties<br />
<a href="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_1.jpg#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="alignnone size-medium wp-image-238" title="SQLSSL_1" src="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_1-300x209.jpg" alt="" width="300" height="209" /></a> <a href="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_2.jpg#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="alignnone size-medium wp-image-239" title="SQLSSL_2" src="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_2-300x207.jpg" alt="" width="300" height="207" /></a></li>
<li>Type in the friendly name as the fully qualified host name, for me, this seemed to even require including proper capitalization because my server name was SRVSQL01.domain.loc, so that is that I used.  I left description blank<br />
<a href="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_3.jpg#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="alignnone size-medium wp-image-240" title="SQLSSL_3" src="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_3-300x266.jpg" alt="" width="300" height="266" /></a></li>
<li>On the Subject Tab, I added the values in the screenshot below.<br />
<a href="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_4.jpg#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="alignnone size-medium wp-image-241" title="SQLSSL_4" src="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_4-300x269.jpg" alt="" width="300" height="269" /></a></li>
<li>On the Extensions Tab, I added &#8220;Key encipherment&#8221;  under the Key Usage setting box, and &#8220;Server Authentication&#8221; and &#8220;Client Authentication&#8221; under the Extended Key Usage (application policies) settings box.<br />
<a href="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_5.jpg#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="alignnone size-medium wp-image-242" title="SQLSSL_5" src="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_5-300x247.jpg" alt="" width="300" height="247" /></a> <a href="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_6.jpg#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="alignnone size-medium wp-image-243" title="SQLSSL_6" src="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_6-300x245.jpg" alt="" width="300" height="245" /></a></li>
<li>On the private key tab, under key type, I changed the value from Exchange to Signature.<br />
<a href="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_7.jpg#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="alignnone size-medium wp-image-244" title="SQLSSL_7" src="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_7-300x248.jpg" alt="" width="300" height="248" /></a></li>
<li>Finally, click OK, then click Next back in the Enrollment Wizard window.</li>
<li>Enter a file name and click Finish.<br />
<a href="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_8.jpg#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="alignnone size-medium wp-image-245" title="SQLSSL_8" src="http://goodbadtech.com/wp-content/uploads/2011/07/SQLSSL_8-300x211.jpg" alt="" width="300" height="211" /></a></li>
</ul>
<h3>2. Process the CSR on your Certificate Server</h3>
<p>I won&#8217;t go into detail here, but you need to copy the file you created to your Certificate Authority server, process and approve the request, then export the binary key file of the certificate.  Then copy that exported binary file back to your SQL server.</p>
<h3>3. Import the certificate into the local certificate store</h3>
<p>Back in your MMC console and Certificates snap-in, you can now right-click on the Personal folder again and select Import.  Complete the import wizard using your recently created binary export of the cerficate and the new cert should now show up in the certificates folder under Personal in the Certificates snap-in.</p>
<p>One last step here, and<strong> its and important one</strong>, on the certificate itself, right-click on the cert name, and select All Tasks -&gt; &#8220;Manage Private Keys&#8230;&#8221;, then give the user the SQLSERVER service runs as Read permission in the security tab.</p>
<h3>4. Tell SQL Server which certificate to use</h3>
<p>Now, when you follow Microsoft&#8217;s instructions and you open the properties of the protocols instance for your SQL Server and view the Certificates tab, you should see the new certificate in the drop down menu! Select the certificate here, click OK, then restart the SQL service.  Clients can now use the encrypted connection and you won&#8217;t see errors like, &#8220;SSL Provider, error: 0 &#8211; The certificate&#8217;s CN name does not match the passed value&#8221;</p>
<p>I hope this saves some of you some time, I spent the better part of 3 days working on this.  I went through the whole process many times, and for me, I believe the biggest change I made was changing the Exchange key type to Signature.  I don&#8217;t know for sure if this is true, but that&#8217;s what I&#8217;m thinking.  In the comments below, let me know if the process works for you or if you have any questions I&#8217;ll be glad to help where I can.</p>
]]></content:encoded>
			<wfw:commentRss>http://goodbadtech.com/2011/07/15/sql-encryption/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Review of iPhone OS 3.0&#039;s Stereo Bluetooth (A2DP)</title>
		<link>http://goodbadtech.com/2009/06/17/iphone-os-30-stereo-bluetooth-a2dp/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://goodbadtech.com/2009/06/17/iphone-os-30-stereo-bluetooth-a2dp/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 21:59:23 +0000</pubDate>
		<dc:creator>goodbadtech</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Bluetooth]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Plantronics]]></category>

		<guid isPermaLink="false">http://goodbadtech.com/?p=98</guid>
		<description><![CDATA[I know there will be lots of immediate feedback as soon as the new iPhone 3.0 OS is rolled out today.  I wanted to get some feedback out right away on one thing I've really been looking for, the stereo bluetooth support.]]></description>
			<content:encoded><![CDATA[<p>I know there will be lots of immediate feedback as soon as the new iPhone 3.0 OS is rolled out today.  I wanted to get some feedback out right away on one thing I&#8217;ve really been looking for, the <a href="http://en.wikipedia.org/wiki/Bluetooth_profile#Advanced_Audio_Distribution_Profile_.28A2DP.29" target="_blank">stereo bluetooth</a> support.  <img class="alignleft size-medium wp-image-99" style="border: 0px none; margin: 13px;" title="p590" src="http://goodbadtech.com/wp-content/uploads/2009/06/p590-264x300.jpg" alt="p590" width="111" height="126" />On the computer at home I have a pair of <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2Fs%3Fie%3DUTF8%26x%3D0%26ref%255F%3Dnb%255Fss%255Fb%26y%3D0%26field-keywords%3Dplantronics%2520590%26url%3Dsearch-alias%253Daps&amp;tag=phot011-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=390957">Plantronics P590</a><img style="border:none !important; margin:0px !important;" src="https://www.assoc-amazon.com/e/ir?t=phot011-20&amp;l=ur2&amp;o=1" border="0" alt="" width="1" height="1" /> headphones.  They are great bluetooth headphones, but I&#8217;ve never taken the time to use the phone features on my <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2Fs%3Fie%3DUTF8%26x%3D0%26ref%255F%3Dnb%255Fss%255Fgw%26y%3D0%26field-keywords%3Diphone%25203g%26url%3Dsearch-alias%253Daps&amp;tag=phot011-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=390957">iPhone</a><img style="border:none !important; margin:0px !important;" src="https://www.assoc-amazon.com/e/ir?t=phot011-20&amp;l=ur2&amp;o=1" border="0" alt="" width="1" height="1" /> because what&#8217;s the point of wearing an ear piece that I can&#8217;t listen to music on??  After I got the new iPhone OS installed today I paired up the P590&#8242;s and tested out the audio quality and the effects on battery life.<span id="more-98"></span></p>
<h3><strong>Audio Quality</strong></h3>
<p>Starting off with <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2Fs%3Fie%3DUTF8%26x%3D0%26ref%255F%3Dnb%255Fss%255Fgw%26y%3D0%26field-keywords%3Dgreen%2520day%26url%3Dsearch-alias%253Daps&amp;tag=phot011-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=390957">Green Day</a><img style="border:none !important; margin:0px !important;" src="https://www.assoc-amazon.com/e/ir?t=phot011-20&amp;l=ur2&amp;o=1" border="0" alt="" width="1" height="1" /> I sat down about two feet away from the phone, the audio was perfect.  I decided to hit up <a title="Google Charts" href="http://code.google.com/apis/chart/" target="_blank">Google Charts</a> to show how the audio quality was as I stepped away from the phone. Overall I was very impressed.  I actually had to go outside to find enough room to really test the signal. Note: The audio quality was measured scientifically by me deciding how well I could hear the music compared to how well I thought I should be able to hear it.</p>
<div style="width: 100%; text-align: center;"><img style="border:1px solid black;" title="Audio Quality Over Distance" src="http://chart.apis.google.com/chart?chs=450x200&amp;cht=ls&amp;chco=334DD2&amp;chm=B,334DD2,0,0,0&amp;chtt=Audio+Quality+Over+Distance|(in+steps)&amp;chd=t:100,100,100,100,100,100,100,100,100,100,100,100,100,50,0,0&amp;chxl=0:|0|2|4|6|8|10|12|14|16|18|20|22|24|26|28|30|&amp;chxt=x,y&amp;chxr=1,0,100" alt="" /></div>
<p>I also ran an indoor/outdoor test.  With the phone left inside, I was able to take about 8 steps to the door, and then 4 or 5 steps outside before the audio cut out.  One last comment, I&#8217;m not sure if this is standard or not, but I had to get back within 10 feet or so to reestablish a reliable audio connection after I got out of range.  I figured I should test the phone call audio quality too, just to make sure that was still okay.  I decided to call my good friend the <a title="The Billionaire Girl" href="http://thebillionairegirl.com/" target="_blank">Billionaire Girl</a>.  Here is our conversation:</p>
<ul>
<li>Me &#8220;Hey Billionaire Girl, Can you hear me now?&#8221;</li>
<li>BG &#8220;Yeah, what do you want? I&#8217;m crazy busy.&#8221;</li>
<li>Me &#8220;I&#8217;m testing the stereo bluetooth functionality on the new iPhone OS 3.0&#8243;</li>
<li>BG &#8220;I&#8217;m jealous, I just have this Blackberry junk that always seems to fall apart&#8221;</li>
<li>Me &#8220;Yeah, that sucks.  Okay, bye.&#8221;</li>
<li>BG &#8220;You suck.&#8221;</li>
</ul>
<p>The Billionaire Girl is amazing, and I suggest you subscribe to her blog, but she hates iPhone talk.  At any rate, I&#8217;d say the audio quality for the phone works as expected, as well as switching over from iPod to iPhone and back within your Bluetooth headset.</p>
<h3><strong>Battery Life</strong></h3>
<p>I gave both my iPhone and Headphones a full charge then I let the music play all afternoon with the exception of about 30 minutes in phone use, a couple text messages, and very limited web browsing.  Note: My email syncs every hour and I have Wi-Fi, Location Services, 3G, and of course Bluetooth enabled.  &#8230; A couple hours in, things are looking pretty good.  The battery is definitely going down, but it doesn&#8217;t appear to be draining any faster than using the iPod with the included wired headphones.  &#8230; Three and a half hours in I got the 20% Battery remaining warning.  I think we&#8217;re fading fast now.  &#8230; Almost exactly 5 hours in my phone went dead. What are your thoughts, is 5 hours a respectable amount of time?</p>
<h3><strong>Conclusion</strong></h3>
<p>This feature was long overdue in my opinion.  I&#8217;m very happy it&#8217;s here, I just don&#8217;t like wires, but it seems that I&#8217;ll need charging wires to get through a day of work.  Maybe the 3G s will fare better on the batter.  In general, I noticed as I was listening throughout the afternoon that my audio would peridocially &#8220;skip&#8221; like a CD skipping in random places.  I wonder if that is just bluetooth cutting out for a moment here and there.   The iPhone felt a little snappier than it did yesterday, if that&#8217;s from the upgrade to the 3.0 OS I&#8217;m happy to take any speed improvements I can get.  Last, don&#8217;t miss any of the new 3.0 features, <a href="http://www.ismashphone.com/2009/06/how_to_use_best_40_features_of_iphone_3.html" target="_blank">here is a great write up I came across today</a> from the folks over at ismashphone.com.</p>
]]></content:encoded>
			<wfw:commentRss>http://goodbadtech.com/2009/06/17/iphone-os-30-stereo-bluetooth-a2dp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails: Import CSV data into database</title>
		<link>http://goodbadtech.com/2009/05/13/ruby-on-rails-import-csv-data-into-database/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://goodbadtech.com/2009/05/13/ruby-on-rails-import-csv-data-into-database/#comments</comments>
		<pubDate>Thu, 14 May 2009 04:24:59 +0000</pubDate>
		<dc:creator>goodbadtech</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Postgre SQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://goodbadtech.com/?p=43</guid>
		<description><![CDATA[Today I needed to migrate data from a Drupal site to Ruby on Rails. It is pretty easy to export data from my Drupal database (MySQL) into CSV format that I can use to import into my Rails application (Postgre SQL).]]></description>
			<content:encoded><![CDATA[<p>Today I needed to migrate data from a <a href="http://drupal.org" target="_blank">Drupal</a> site to Ruby on Rails.  It is pretty easy to export data from my Drupal database (<a href="http://www.mysql.com" target="_blank">MySQL</a>) into CSV format that I can use to import into my Rails application (<a href="http://www.postgresql.org" target="_blank">Postgre SQL</a>).  So that&#8217;s where I started.  I&#8217;m running Drupal 6.x, here is my database query to pull nodes out of the database.<span id="more-43"></span> I ran this in <a href="http://www.phpmyadmin.net" target="_blank">phpMyAdmin</a> then exported the results to a CSV file.</p>
<pre class="brush: sql">SELECT * FROM www_node n
	INNER JOIN www_content_type_release t
	INNER JOIN www_location l
	INNER JOIN www_location_instance i
	INNER JOIN www_node_revisions r
	ON t.nid=n.nid AND n.nid=i.nid AND i.lid=l.lid AND r.nid=n.nid
	WHERE n.type="release"</pre>
<p>Back in Rails, I already have my application setup to support the press releases I&#8217;ll be importing and I won&#8217;t be covering that code here, it&#8217;s a standard scaffolding type MVC. You&#8217;ll need to update the code below to match your application naming.</p>
<hr />First, we need a model to store the uploaded file and some related information. To upload and attach the CSV data, I&#8217;m going to use the <a href="http://www.thoughtbot.com/projects/paperclip" target="_blank">Paperclip plugin</a>, thanks to <a href="http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/" target="_blank">jimneath.org</a> for the blog post on integrating Paperclip into your rails application. If you have more questions on Paperclip, please refer to their documentation or jimneath.org.</p>
<pre class="brush: ruby">script/plugin install git://github.com/thoughtbot/paperclip.git</pre>
<p>Continuing on with the code, you&#8217;ll need to generate a model for you data.</p>
<pre class="brush: ruby">ruby script/generate model import</pre>
<p>Edit the model import.rb</p>
<pre class="brush: ruby">class Import &lt; ActiveRecord::Base
  # Paperclip
  has_attached_file :csv
  validates_attachment_presence :csv
	validates_attachment_content_type :csv, :content_type =&gt; ['text/csv','text/comma-separated-values','text/csv','application/csv','application/excel','application/vnd.ms-excel','application/vnd.msexcel','text/anytext','text/plain']
end</pre>
<p>Edit the migration</p>
<pre class="brush: ruby">class CreateImports &lt; ActiveRecord::Migration
	def self.up
		create_table :imports do |t|
			t.string :datatype
			t.integer :processed, :default =&gt; 0
			t.string :csv_file_name
			t.string :csv_content_type
			t.integer :csv_file_size
			t.timestamps
		end
	end

	def self.down
		drop_table :imports
	end
end</pre>
<p>Update your database:</p>
<pre class="brush: ruby">rake db:migrate</pre>
<hr />Next, lets generate a controller to upload files and process the data</p>
<pre class="brush: ruby">ruby script/generate controller imports</pre>
<p>In imports_controller.rb I&#8217;m going to add methods to process the data I&#8217;ll be importing as well as a private methods to read the CSV file and store the records in the database. Note: I installed the gem, fastercsv | http://fastercsv.rubyforge.org/ to parse the CSV data.</p>
<pre class="brush: ruby">class ImportsController &lt; ApplicationController
  before_filter :login_required #protect controller from anonymous users

  def new
    @import = Import.new
	end

	def create
    @import = Import.new(params[:import])

    respond_to do |format|
      if @import.save!
        flash[:notice] = 'CSV data was successfully imported.'
        format.html { redirect_to(@import) }
      else
        flash[:error] = 'CSV data import failed.'
        format.html { render :action =&gt; "new" }
      end
    end
	end

	def show
    @import = Import.find(params[:id])
	end

	def proc_csv
    @import = Import.find(params[:id])
    lines = parse_csv_file(@import.csv.path)
    lines.shift #comment this line out if your CSV file doesn't contain a header row
    if lines.size &gt; 0
      @import.processed = lines.size
      lines.each do |line|
        case @import.datatype
        when "releases"
          new_release(line)
        end
      end
      @import.save
      flash[:notice] = "CSV data processing was successful."
      redirect_to :action =&gt; "show", :id =&gt; @import.id
    else
      flash[:error] = "CSV data processing failed."
      render :action =&gt; "show", :id =&gt; @import.id
    end
	end

private

  def parse_csv_file(path_to_csv)
    lines = []

    #if not installed run, sudo gem install fastercsv
    #http://fastercsv.rubyforge.org/
    require 'fastercsv'

    FasterCSV.foreach(path_to_csv) do |row|
      lines &lt;&lt; row
    end
    lines
  end

	def new_release(line)
    params = Hash.new
    params[:release] = Hash.new
    params[:release]["title"] = line[0]
		params[:release]["rdate"] = line[1]
    params[:release]["body"] = line[3]
    params[:release]["notes"] = line[4]
    release = Release.new(params[:release])
    release.save
	end

end</pre>
<hr />Last, we need to create two views, the first allows you to upload the CSV file, the second shows the results of the upload and allows you to process the data. Note: At this point you should probably <strong>reload your web server</strong> so the fastercsv gem and paperclip plugin are available.</p>
<p>new.html.erb</p>
<pre class="brush: ruby">
<h2>Upload a CSV file to import into the database</h2>

&lt;% form_for @import, :html =&gt; { :multipart =&gt; true } do |f| %&gt;

  &lt;%= f.file_field :csv %&gt;
&lt;select name="import[datatype]" size="1"&gt;
  &lt;option value="releases"&gt; Press Releases &lt;/option&gt;
  &lt;/select&gt;

    &lt;%= f.submit "Import" %&gt;

&lt;% end %&gt;</pre>
<p>show.html.erb</p>
<pre class="brush: ruby">
<h2>Results of your CSV Upload</h2>

&lt;%= @import.csv.path %&gt;

&lt;% if @import.processed &gt; 0 %&gt;

Success! This import contained &lt;%=  @import.processed %&gt; records

&lt;% else %&gt;

&lt;%= link_to "process", import_proc_path(@import.id) %&gt;

&lt;% end %&gt;</pre>
<p>Update your routes.rb, add the following line:</p>
<pre class="brush: ruby">map.resources :imports
map.import_proc '/import/proc/:id', :controller =&gt; "imports", :action =&gt; "proc_csv"</pre>
<p>That&#8217;s pretty much it.  You can alter/expand on this to support multiple models in your application.  Just make sure to pay close attention to detail when your mapping columns in the CSV file to values in your model.  You may want to add a call somewhere to destroy old CSV import records since you likely don&#8217;t need to store the CSV data indefinately.  Also, there could be a lot more error checking to this process to make sure the upload is valid data and each row is processed correctly.  I hope to follow up this post soon with a Part 2 which adds more data processing details.  Last, I know there is a lot of stuff placed in the controller, it&#8217;s something i&#8217;ve been in the habit of doing.  Any input on moving processing to the model would be welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://goodbadtech.com/2009/05/13/ruby-on-rails-import-csv-data-into-database/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
	</channel>
</rss>

