<?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; Programming</title>
	<atom:link href="http://goodbadtech.com/category/programming/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>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>
		<item>
		<title>Using VMWare to install Ruby on a CentOS5 guest</title>
		<link>http://goodbadtech.com/2009/05/12/using-vmware-to-install-ruby-on-a-centos5-guest/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://goodbadtech.com/2009/05/12/using-vmware-to-install-ruby-on-a-centos5-guest/#comments</comments>
		<pubDate>Tue, 12 May 2009 14:25:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Virtualization]]></category>
		<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[VMWare]]></category>

		<guid isPermaLink="false">http://goodbadtech.com/?p=13</guid>
		<description><![CDATA[Recently I installed CentOS 5 as a guest host on my VMWare server to run a RoR development environment.  This the process I went through.  Much thanks to a post on the Rubyonrails.com wiki that got me going in the right direction. CentOS 5 Download the NetInstall ISO of CentOS 5.2 I created my guest [...]]]></description>
			<content:encoded><![CDATA[<div class="snap_preview">
<p>Recently I installed CentOS 5 as a guest host on my VMWare server to run a RoR development environment.  This the process I went through.  Much thanks to a <a rel="#someid0" href="http://wiki.rubyonrails.org/rails/pages/RailsOnCentos5_mongrel" target="_blank">post on the Rubyonrails.com wiki</a> that got me going in the right direction.</p>
<p align="center"><img class="size-full wp-image-24" title="Ruby On Rails" src="http://goodbadtech.com/wp-content/uploads/2009/05/ror.jpg" alt="ror" width="103" height="122" /> <img class="size-full wp-image-23" title="CentOS" src="http://goodbadtech.com/wp-content/uploads/2009/05/centos_logo_45.png" alt="centos_logo_45" width="208" height="45" /> <img class="size-full wp-image-22" title="VMWare" src="http://goodbadtech.com/wp-content/uploads/2009/05/logo_vmware.jpg" alt="logo_vmware" width="190" height="130" /></p>
<h2><span id="more-13"></span>CentOS 5</h2>
<ol>
<li>Download the NetInstall ISO of <a rel="#someid1" href="http://isoredirect.centos.org/centos/5/isos/i386/" target="_blank">CentOS 5.2</a></li>
<li>I created my guest host using most of the basic Linux settings, 256 MB RAM and an 8GB hard drive (the great thing about VMWare is those values are so easy to change later</li>
<li>I set the VMWare configuration to boot off of the CentOS netinstall ISO and started it up
<ol>
<li>I am installing the text version of CentOS only</li>
<li>Select English language and US keyboard</li>
<li>Installation source -&gt; HTTP</li>
<li>Enable IPv4 w/ DHCP was selected by default, I left that setting in place</li>
<li>HTTP Setup
<ol>
<li>Website Name: mirror.centos.org</li>
<li>CentOS Directory: centos-5/5.2/os/i386</li>
</ol>
</li>
<li>Initialize the hard drive</li>
<li>I selected “Remove all partitions on selected drives and create default layout” because I’m not that concerned with the specific directory structure and I can always add another virtual hard disk if I need more space.</li>
<li>I got a memory warning that there isn’t a lot of memory on my machine, and a swap file is required right away.  I selected, “Yes”</li>
<li>Use GRUB as the book loader</li>
<li>I didn’t add any additional GRUB options or a GRUB password</li>
<li>I left only my CentOS install as the only OS to attempt to boot</li>
<li>Boot Loader goes in the MBR</li>
<li>Configure the eth0 network interface = yes</li>
<li>I turned off IPv6</li>
<li>DHCP IP address</li>
<li>Set my timezone</li>
<li>Set the root password</li>
<li>Software selection -&gt; I selected “Customize Software Selection”
<ol>
<li>Remove everything but Base</li>
<li>Done</li>
</ol>
</li>
<li>Thanks to Comcast, my 540 MB netinstall took less than 10 minutes.</li>
<li>Before you reboot your VM, edit the CD-ROM settings so it’s not connected and is set NOT to connect at power on.</li>
<li>Reboot</li>
<li>After the reboot completes, you will be prompted with some additional configuration options.  These are really up to you. I edited:
<ol>
<li>The firewall settings, leaving it enabled, but opening ports 22, 80, 443, and 1900 (for RoR)</li>
<li>Network settings to use a static IP address</li>
<li>Some of the system services and turned off stuff I don’t need</li>
</ol>
</li>
<li>Login to localhost</li>
<li>type, yum update, to get your system fully up to date</li>
<li>There are a number of additional configuration steps to finish the CentOS install, but what exactly you do from here will be up to you.  I recommend at least creating a user so you can login as someone other than root, and update your SSH settings so that root cannot login.</li>
<li>Next, I change from the VMWare console to an SSH client (keep in mind, that without completing your OS install properly you really are running the server at a greater risk level)…</li>
</ol>
</li>
</ol>
<h2>Ruby on Rails</h2>
<ol>
<li>at the command prompt in your ssh client run, yum -y install ruby ruby-rdoc ruby-devel mysql-devel gcc</li>
<li>next run (check http://rubyforge.org/projects/rubygems/ for latest version), wget http:<code>//rubyforge.rubyuser.de/rubygems/rubygems-1.3.1.tgz</code></li>
<li>tar xzf rubygems-1.3.1.tgz</li>
<li>cd rubygems-1.3.1</li>
<li>ruby setup.rb</li>
<li>gem update –system</li>
<li>gem install mysql –no-rdoc –no-ri — –with-mysql-config=/usr/bin/mysql_config</li>
<li>gem install mongrel_cluster –no-rdoc –no-ri</li>
<li>/usr/sbin/adduser -r mongrel</li>
<li>mkdir /etc/mongrel_cluster</li>
<li>cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/</li>
<li>sudo chmod +x /etc/init.d/mongrel_cluster</li>
<li>/etc/init.d/mongrel_cluster start &amp;&amp; /sbin/chkconfig mongrel_cluster on</li>
<li>mkdir /var/www</li>
<li>mkdir /var/www/apps</li>
</ol>
<p>That should get you far enough to install your first Ruby on Rails app.  I’m sure I’ll follow up this post with more details about that part of the process.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://goodbadtech.com/2009/05/12/using-vmware-to-install-ruby-on-a-centos5-guest/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Hello World</title>
		<link>http://goodbadtech.com/2009/05/11/hello/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://goodbadtech.com/2009/05/11/hello/#comments</comments>
		<pubDate>Mon, 11 May 2009 13:41:34 +0000</pubDate>
		<dc:creator>goodbadtech</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Big Omaha]]></category>

		<guid isPermaLink="false">http://goodbadtech.com/?p=5</guid>
		<description><![CDATA[As my first post on this blog, I should probably dig into a recent packet capture session and talk about how I found a firewall bug that was sending ARP requests on a L2 Bridge out the wrong interface, but I had the great opportunity to attend the first Big Omaha conference this week in [...]]]></description>
			<content:encoded><![CDATA[<p>As my first post on this blog, I should probably dig into a recent packet capture session and talk about how I found a firewall bug that was sending ARP requests on a L2 Bridge out the wrong interface, but I had the great opportunity to attend the first <a title="Big Omaha" href="http://www.bigomaha.com/" target="_blank">Big Omaha </a>conference this week in Omaha, NE and wanted to start off with a few post conference thoughts.</p>
<p>For me, the main takeaway was further justification of an idea I already believed in, that the best way to advance yourself in life, professionally and personally, is to be true to who you are.  There are so many ways to be successful, but the only one that will work for you is to follow the one that matches your thinking and your personality.  Everything else will follow behind that.</p>
<p>One of the more interesting themes that ran throughout the conference was the meaning of certain words, failure, loser, entrepreneur, and a few others I think. It started right from the get go when Jason Fried @ <a title="37signals.com" href="http://37signals.com" target="_blank">37signals.com</a> opened the conference by walking on stage and saying, &#8220;I hate that people think they have to fail first to succeed later.&#8221;  His main point was to focus on repeating successes instead of trying to alter your actions from past failures to see if it works better the next time.</p>
<p>I thought it was great commentary.  I likened it to Tiger Woods, or LeBron James.  The most dominant athletes in the world continue to dominate because when they go up for a shot, or line up a putt, they are 100% sure it&#8217;s going in because the only thing in their head is the last make they had in the exact same scenario.  Misses, or trying something different than the last miss, don&#8217;t take up any room in their thought process.  In their head its a guaranteed make.</p>
<p><object width="425" height="344" data="http://www.youtube.com/v/QHsfMTdfsJ0&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/QHsfMTdfsJ0&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /></object></p>
<p>It&#8217;s a great mentality to carry with you everywhere in life.  Find your successes in life and repeat.</p>
<p>I hear the video from the conference will be online soon, I&#8217;ll probably post a few clips here as soon as it is.</p>
<p>Oh, and welcome to /Good/Bad/Tech</p>
]]></content:encoded>
			<wfw:commentRss>http://goodbadtech.com/2009/05/11/hello/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

