<?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>Webdevotion.be &#187; ruby</title>
	<atom:link href="http://www.webdevotion.be/blog/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webdevotion.be/blog</link>
	<description>Trainer for the Flash Platform and iOS</description>
	<lastBuildDate>Tue, 01 Nov 2011 10:14:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Change a column type when using Postgresql in Rails migrations</title>
		<link>http://www.webdevotion.be/blog/2010/06/16/change-a-column-type-when-using-postgresql-in-rails-migrations/</link>
		<comments>http://www.webdevotion.be/blog/2010/06/16/change-a-column-type-when-using-postgresql-in-rails-migrations/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 15:32:23 +0000</pubDate>
		<dc:creator>Webdevotion</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[:boolean]]></category>
		<category><![CDATA[:text]]></category>
		<category><![CDATA[change_column]]></category>
		<category><![CDATA[heroku]]></category>
		<category><![CDATA[migration]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://webdevotion.be/blog/?p=334</guid>
		<description><![CDATA[When using Rails, you're using migrations a lot.  Migrations are awesome.  But today I ran into a problem.  I had to convert a column to be of type :boolean ( it was of type :text ).  I was suprised when things went wrong. <a href="http://www.webdevotion.be/blog/2010/06/16/change-a-column-type-when-using-postgresql-in-rails-migrations/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When using Rails, you&#8217;re using <a href="http://guides.rubyonrails.org/migrations.html">migrations</a> a lot.  Migrations are awesome.  But today I ran into a problem.  I had to convert a column to be of type :boolean ( it was of type :text ).  I was suprised when things went wrong.<br />
<span id="more-334"></span><br />
When running the migration I got this error from Postgresql:<br />
<code>cannot be cast to type "pg_catalog.bool"</code></p>
<p>Apparantly, Postgresql can&#8217;t convert a text column into a boolean, even when passing default values.  The standard way of writing the migration didn&#8217;t work:<br />
<code>change_column :projects, :status, :boolean, :default=>true</code></p>
<p>So I started googling and ended up with:<br />
<code>execute "alter table projects ALTER COLUMN status TYPE boolean USING CASE status WHEN '1' THEN true ELSE false END;"</code></p>
<p>While this line did work, there is a problem with it; it&#8217;s not agnostic.  It&#8217;s written specifically for Postgresql, but may cause issues in other environments.</p>
<p>So, the final, agnostic solution ( <a href="http://twitter.com/simonmenke">thanks Simon</a>! )  uses plain Rails code.  Basically we add a temporary column of type :boolean to store the information.  After looping over all Project records we have converted all text values to the type boolean.  Because we have all information stored in the temporary column, it&#8217;s ok to remove the existing status column and rename convert_status to status.</p>
<pre><code>class ConvertStatusToBoolean < ActiveRecord::Migration
  def self.up
    add_column :projects, :convert_status, :boolean, :default => true

    # look up the schema's to be able to re-inspect the Project model
    # http://apidock.com/rails/ActiveRecord/Base/reset_column_information/class
    Project.reset_column_information

    # loop over the collection
    Project.all.each do |p|
        p.convert_status = p.status == '1'
        p.save
    end

    # remove the older status column
    remove_column :projects, :status
    # rename the convert_status to status column
    rename_column :projects,:convert_status,:status
  end

  def self.down
    change_column :projects, :status, :text
  end
end</code></pre>
<p>Handy cheat sheet:<br />
<a href="http://dizzy.co.uk/ruby_on_rails/cheatsheets/rails-migrations#execute">http://dizzy.co.uk/ruby_on_rails/cheatsheets/rails-migrations#execute</a></p>
<p>Useful info on Apidock:<br />
<a href="http://apidock.com/rails/ActiveRecord/Base/reset_column_information/class">http://apidock.com/rails/ActiveRecord/Base/reset_column_information/class</a><br />
<br/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.webdevotion.be/blog/2010/06/16/change-a-column-type-when-using-postgresql-in-rails-migrations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Another job posting for www.mrhenry.be</title>
		<link>http://www.webdevotion.be/blog/2009/03/05/web-html-developer-vacature-mrhenry-antwerpen/</link>
		<comments>http://www.webdevotion.be/blog/2009/03/05/web-html-developer-vacature-mrhenry-antwerpen/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 20:10:36 +0000</pubDate>
		<dc:creator>Webdevotion</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[antwerpen]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mr henry]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[vacature]]></category>

		<guid isPermaLink="false">http://webdevotion.be/blog/?p=240</guid>
		<description><![CDATA[We have another position available at Mr. Henry. This time we&#8217;re looking out for someone who&#8217;s good with HTML, CSS and JavaScript. Added bonus points if your also proficient with PHP and / or Ruby on Rails. Go Go Go!]]></description>
			<content:encoded><![CDATA[<p>We have <a href="http://webdevotion.be/blog/2009/02/25/job-opening-at-mr-henry/">another position</a> available at Mr. Henry.<br />
This time we&#8217;re looking out for someone who&#8217;s good with HTML, CSS and JavaScript.</p>
<p>Added bonus points if your also proficient with PHP and / or Ruby on Rails.</p>
<p><a href="http://workingat.mrhenry.be">Go Go Go!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.webdevotion.be/blog/2009/03/05/web-html-developer-vacature-mrhenry-antwerpen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

