<?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>Thinking In Rails &#187; Tips</title>
	<atom:link href="http://thinkinginrails.com/category/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkinginrails.com</link>
	<description>A Perl Programmer&#039;s Exploration of The World of Ruby on Rails</description>
	<lastBuildDate>Thu, 12 Apr 2012 03:37:58 +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>Using VIM In Your Workflow</title>
		<link>http://thinkinginrails.com/2010/08/using-vim-in-your-workflow/</link>
		<comments>http://thinkinginrails.com/2010/08/using-vim-in-your-workflow/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 17:40:04 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[Links]]></category>
		<category><![CDATA[Resources]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://thinkinginrails.com/?p=610</guid>
		<description><![CDATA[After a bit of a wait, my buddy Curtis McHale has posted the article I wrote for his site, yourworkflow.ca.  You can check out the Using Vim As A Text Editor article over on the site.  It&#8217;s not just about Vim, but more the day to day coding workflow that I was using, so it [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-611" title="vim-editor_logo-150x150" src="http://thinkinginrails.com/wp-content/uploads/2010/08/vim-editor_logo-150x150.png" alt="" width="150" height="150" />After a bit of a wait, my buddy <a href="http://twitter.com/curtismchale">Curtis McHale</a> has posted the article I wrote for his site, <a href="http://yourworkflow.ca/">yourworkflow.ca</a>.  You can check out the <a href="http://yourworkflow.ca/2010/08/how-to/using-vim-as-a-text-editor/"><strong>Using Vim As A Text Editor</strong></a> article over on the site.  It&#8217;s not <em>just</em> about Vim, but more the day to day coding workflow that I was using, so it includes things like Firefox plugins, Unix utilities, etc.</p>
<p>So head over and check out the workflow site and read not only my <em>fine</em> fine article, but also the other news and podcasts he&#8217;s got going on over there.</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkinginrails.com/2010/08/using-vim-in-your-workflow/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Anatomy of A Ruby Gem</title>
		<link>http://thinkinginrails.com/2010/05/anatomy-of-a-ruby-gem/</link>
		<comments>http://thinkinginrails.com/2010/05/anatomy-of-a-ruby-gem/#comments</comments>
		<pubDate>Fri, 07 May 2010 03:01:22 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[Help]]></category>
		<category><![CDATA[Instructions]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://thinkinginrails.com/?p=431</guid>
		<description><![CDATA[If you&#8217;ve ever installed a Ruby gem off of gemcutter or github, you&#8217;ll know that you run the gem install &#60;gemname&#62; command and when it&#8217;s completed you magically have access to use it through &#8216;require &#60;gemname&#62;&#8217;.  If you&#8217;ve ever looked into the source you might want to have an idea of what goes where and [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever installed a Ruby gem off of gemcutter or github, you&#8217;ll know that you run the gem install &lt;gemname&gt; command and when it&#8217;s completed you magically have access to use it through &#8216;require &lt;gemname&gt;&#8217;.  If you&#8217;ve ever looked into the source you might want to have an idea of what goes where and how it all works.</p>
<p>First of all, I&#8217;m fairly new to this myself, so it&#8217;s very possible that I&#8217;ll miss something vital, please feel free to flame me in the comments, as long as you tell me where I messed up so I can fix it <img src='http://thinkinginrails.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-431"></span></p>
<p>Lets take a couple of examples.  One is a gem for dissecting XML feeds that I just discovered, <a href="http://github.com/pauldix/feedzirra">feedzirra</a>, the other is a bit more complex, <a href="http://github.com/injekt/cinch">Cinch</a>, the IRC micro-framework I was talking about a few days ago.  Starting with feedzirra, here&#8217;s the listing of the files and directories in the root of the gem, I&#8217;ll take them one by one.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">README.rdoc
README.textile
Rakefile
feedzirra.gemspec
lib/
spec/</pre></div></div>

<p><strong>README.*</strong></p>
<p>The first two are pretty obvious, they are the README files with GitHub (and others) automatically parses out and displays when you hit the main project page.  Here is a <a href="http://github.com/guides/readme-formatting">list of the README formats that github supports</a>, as well the order that it will display them in.  Here the author has included a README file as well as a README.textile (<a href="http://textile.thresholdstate.com/">texttile</a> is a simple markup language), and github displays the REAMDE.[something] first.</p>
<p><strong>Rakefile</strong></p>
<p><a href="http://rake.rubyforge.org/">Rake</a>, as you know is the ruby version of &#8220;make&#8221; the venerable Unix utility.  Inside the Rakefile you find a <a href="http://rake.rubyforge.org/files/doc/rakefile_rdoc.html">formatted</a> set of rules in the form of tasks and targets.  Hit <a href="http://onestepback.org/index.cgi/Tech/Rake/Tutorial/RakeTutorialRules.red">this tutorial</a> for some more details.  The short story is that there are targets (with prerequisite/dependency information) such as install, uninstall, and release, supporting functions if needed, and some boilerplate info (description, name, contact, etc).  The targets can be anything it seems, for example Feedzirra has a task for releasing and uploading a new release, whereas cinch just has an &#8216;install&#8217; task.  Rake is used a lot to help the creation of gems, but I don&#8217;t think that it&#8217;s used in the actual installation.</p>
<p><strong>[gemname].gemspec</strong></p>
<p>Here is the meat of it all, the gemspec file consists of the control information for the entire gem.  If you check out <a href="http://asciicasts.com/episodes/183-gemcutter-jeweler">this tutorial</a> you&#8217;ll see that it&#8217;s ruby code and can be created with a few utility scripts including Rake, and has some of the following information:</p>
<ul>
<li>Gem name and information (homepage, description)</li>
<li>Author information (email, etc)</li>
<li>Version and Date</li>
<li>Other gem dependencies</li>
<li>One or more boolean flags (ie: is there documentation that needs to be generated)</li>
<li>A file list of what files need to be installed</li>
<li>A list of executables of files that will be put in the bin directory when they&#8217;re installed</li>
</ul>
<p><strong>lib/ And Other Files or Directories</strong></p>
<p>Basically what&#8217;s left are directories of files that are going to be installed on your system.  Of course, &#8220;lib&#8221; isn&#8217;t always going to be there, but for most gems that are adding a new class into your ruby environment, it will be.  The gemspec file has the list of the files and directories that will actually be taken out of the gem and installed into your system.  There are different ways to specify these files in gemspec, as you&#8217;ll see by comparing the <strong>s.files</strong> directive in the <a href="http://github.com/injekt/cinch/blob/master/cinch.gemspec">Cinch gemspec</a> and the <a href="http://github.com/pauldix/feedzirra/blob/master/feedzirra.gemspec">Feedzirra gemspec</a>.</p>
<p>Inside the lib/ directory there will generally be one or two main files.  In Cinch you have:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">cinch/
cinch.rb</pre></div></div>

<p>This is the module that is loaded when you do a &#8216;require &#8220;cinch&#8221;&#8216; and the cinch/ directory has the supporting files.  The cinch.rb module file has a bunch of requires of &#8220;cinch/irc&#8221;, &#8220;cinch/rbase&#8221;, etc which are the sub-modules that it uses.  More on Modules vs Classes in a later post.</p>
<p><strong>spec/</strong></p>
<p>This will be the specs that the author has put in his source tree and includes so that other developers can look and use their tests.  Again, this isn&#8217;t going to <em>always</em> be there, but it seems to be very in vogue to do this whole &#8220;testing&#8221; thing, and including your spec or test directory is The Right Thing To Do.</p>
<p><strong>File Locations</strong></p>
<p>So when you run &#8216;gem install [foo]&#8216; where do the files go?  Lets use Cinch again for this one as it&#8217;s fairly simple.  Here&#8217;s a listing of the files that are in the source on github:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">./LICENSE
./lib/cinch.rb
./lib/cinch/irc
./lib/cinch/irc/parser.rb
./lib/cinch/* [ more library module files ]
./examples/hello.rb
./examples/* [ more example files ]
./README.rdoc
./spec/irc/parser_spec.rb
./spec/* [ more specs ]
./Rakefile
./cinch.gemspec</pre></div></div>

<p>(Note some of the directories are abbreviated).</p>
<p>When you do a <strong>gem install</strong> the files moved into the path defined in $GEM_HOME thusly:</p>
<pre>cinch/lib           =&gt; $GEM_HOME/gems/cinch-0.2.6/lib
cinch/examples      =&gt; $GEM_HOME/gems/cinch-0.2.6/examples
[...]
cinch/README.rdoc   =&gt; $GEM_HOME/gems/cinch-0.2.6/README.rdoc
                    =&gt; $GEM_HOME/doc/cinch-0.2.6/ri/*
                    =&gt; $GEM_HOME/doc/cinch-0.2.6/rdoc/*
</pre>
<p>Nothing <em>hugely</em> interesting, files are basically just moved into your gem home directory under a directory named with the name and version of the gem you&#8217;re installing.  Note that the doc/*/ri and doc/*/rdoc files are auto-created when the gem install runs, if you don&#8217;t pass the &#8211;no-ri or &#8211;no-rdoc flags of course.  The internal magic in rubygems and ruby allow your programs to find the gems when they are &#8216;required&#8217;.</p>
<p><strong>How to Examine A Gem</strong></p>
<p>One of the nicest ways to play with these things, and what I did was  to use <a href="http://rvm.beginrescueend.com">RVM</a> and then doing a gem  install, as well as using git clone to pull down the source files from  github.  RVM, combined with it&#8217;s great <a href="http://rvm.beginrescueend.com/gemsets/">gemset support</a>, is  a great tool and makes it really easy to add, remove, and play with no  fear of mucking things up.</p>
<p><strong>Conclusion</strong></p>
<p>So that&#8217;s about it for the normal library gems that you&#8217;ll find on github.  Hopefully you now understand a bit more about how a gem is structured and what happens when you install and use gems, and how they will interact with each other.  I found lots of resources out there, including some great stuff on how to create your own gems, gemspec files, and so on, listed below.</p>
<p><strong>Additional Reading</strong></p>
<ul>
<li><a href="http://docs.rubygems.org/read/chapter/5">http://docs.rubygems.org/read/chapter/5</a></li>
<li><a href="http://rubygems.org/pages/gem_docs">http://rubygems.org/pages/gem_docs</a></li>
<li><a href="http://railscasts.com/episodes/135-making-a-gem">http://railscasts.com/episodes/135-making-a-gem</a></li>
<li><a href="http://asciicasts.com/episodes/183-gemcutter-jeweler">http://asciicasts.com/episodes/183-gemcutter-jeweler</a></li>
<li><a href="http://buzaz.com/index.php/2010/01/03/how-to-build-a-ruby-gem/">http://buzaz.com/index.php/2010/01/03/how-to-build-a-ruby-gem/</a></li>
<li><a href="http://docs.rubygems.org/read/chapter/20">http://docs.rubygems.org/read/chapter/20</a></li>
<li><a href="http://yehudakatz.com/2010/04/02/using-gemspecs-as-intended/">http://yehudakatz.com/2010/04/02/using-gemspecs-as-intended/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://thinkinginrails.com/2010/05/anatomy-of-a-ruby-gem/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails Lingo For Beginners</title>
		<link>http://thinkinginrails.com/2010/04/ruby-on-rails-lingo-for-beginners/</link>
		<comments>http://thinkinginrails.com/2010/04/ruby-on-rails-lingo-for-beginners/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 04:44:54 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[Help]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://thinkinginrails.com/?p=326</guid>
		<description><![CDATA[One of the more challenging things starting out in a new language is having to not only learn the new language/framework, but dealing with the jargon that goes along with it.  There&#8217;s nothing more frustrating than finding someone willing to answer your questions, but you can&#8217;t understand what they&#8217;re saying!  Here&#8217;s a few of the [...]]]></description>
			<content:encoded><![CDATA[<p>One of the more challenging things starting out in a new language is having to not only learn the new language/framework, but dealing with the jargon that goes along with it.  There&#8217;s nothing more frustrating than finding someone willing to answer your questions, but you can&#8217;t understand what they&#8217;re saying!  Here&#8217;s a few of the acronyms and terms that you might come across if you&#8217;re a Ruby on Rails newbie, with a perl refugee slant.<img class="alignright" title="Jargon for Ruby on Rails Programmers" src="/images/jargon.jpg" alt="" width="285" height="388" /></p>
<ul>
<li><strong>Gem</strong> &#8211; The basic distribution of most of the plugins and addon libraries for Ruby.  Thing <a href="http://cpan.org">CPAN</a>.  A lot of times you can either install a gem onto your system or as a plugin to a rails project.  The basics are you can run &#8220;gem list&#8221;, &#8220;gem install &lt;gem&gt;&#8221; and &#8220;gem search &#8211;remote &lt;search&gt;&#8221; to list installed gems, install a new gem by name, or search the configured remote gem servers.  Speaking of CPAN, the main two places for gems (currently anyway) are <a href="http://github.com">github</a> and <a href="http://rubygems.org/">rubygems</a>.  The main rubygems distribution is essential to get most of the gem functionality, so you&#8217;ll need to make sure you have the rubygems package (for your OS) installed and working before you can start doing &#8220;gem install&#8221; all crazy like.  More information can be found on <a href="http://docs.rubygems.org/">the rubygems site</a>.</li>
<li><strong>TDD</strong> &#8211; <a href="http://en.wikipedia.org/wiki/Test-driven_development"><strong>T</strong>est <strong>D</strong>riven<strong> D</strong>evelopment</a>.  Ruby and Rails people <em>love</em> testing.  They love it like mom&#8217;s homemade pasta and meatballs served to you with lots of Parmesan cheese after a long day.<em></em> The TDD philosophy basically means that before you write code, you write a test.  In the simplest &#8220;hello world&#8221; case you would write a test to say &#8220;does the program print out &#8216;hello world&#8217;&#8221;.  It of course fails because there is no program written yet.  Then you write the code and work on it until it prints out &#8220;hello world&#8221; and the test passes.  Some people follow TDD to extremes, some ignore it completely (but shouldn&#8217;t) and others work in the grey area in between extremes.</li>
<li><strong>Waterfall</strong> &#8211; The <a href="http://en.wikipedia.org/wiki/Waterfall_model">waterfall model</a> is another design philosophy, but this is more of the old school, not web 2.0, evil-used-by-suits philosophy.  It&#8217;s your classic &#8220;wait till Bob gets the requirements done, then we need the architect team to design the models and system, then we&#8217;ll get you to implement it, it&#8217;ll head into QA for them to test, and eventually it&#8217;ll go into maintenance mode&#8221;.  <em>Yawn</em>.  That&#8217;s not to say that there aren&#8217;t good reasons to use this model, but it&#8217;s considered an old school way of doing things.  You&#8217;ll know what your mentor means and not think he&#8217;s talking about a trip to look at waterfalls anyway.</li>
<li><strong>DHH</strong> &#8211; This of course is the creator of Rails, David Heinemeir Hansson.  You might also hear names like Obie Fernandez, Wayne Seguin, and JEG2 (James Edward Gray II), and these too are people high up in the community.  There are a lot of them, and it&#8217;s a big community, so don&#8217;t feel too bad if you don&#8217;t recognize a name or two.</li>
<li><strong>MVC</strong> &#8211; The <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">Model, View, Controller</a> model is at the core of Rails, and if you somehow managed to miss this in the first chapter of <em>any</em> Rails book, I&#8217;m shocked!  However, if you somehow did, you may hear it thrown around and get confused, especially if you come from another programming language and are still used to the old world &#8220;SQL and code inside your HTML file&#8221; world that you&#8217;ll be <em>so</em> happy to escape.</li>
<li><strong>DRY</strong> &#8211; Another big one I&#8217;d be shocked if you didn&#8217;t know, it&#8217;s <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">Don&#8217;t Repeat Yourself</a>, another tenant of the Ruby and Rails world.  Basically this means that copy and paste programming is not a desired thing, and if you find yourself doing the same bit of code more than once (ie: formatting a price with a $ and decimal places) it&#8217;s best to find a way to move that into it&#8217;s own function so it&#8217;s only in one place.  This makes code easier to maintain, and keeps you happy.  Not a Rails specific philosophy, but definitely a core value.  Next time someone looks at your work and tells you it&#8217;s not DRY you&#8217;ll know what they mean.</li>
<li><strong>Refactoring</strong> &#8211; Closely related to DRY, refactoring is the act of going back and seeing where you can extract common functionality or make the code more idiomatic or simple.  Different from <a href="http://www.c2.com/cgi/wiki?PerlGolf">Golf challenges in Perl</a> by far.  Check out <a href="http://refactormycode.com/">Refactor My Code</a> if you want to get some great examples of how different people take the approach.  This site is <em>awesome</em> as you don&#8217;t just see the end result of good coding (as you&#8217;d see by reading (some) other people&#8217;s source code), but both the start and several alternative results.</li>
<li><strong>Convention over Configuration</strong> &#8211; <a href="http://en.wikipedia.org/wiki/Convention_over_configuration">The idea</a> that by following naming and file conventions, and following them, productivity goes up over a framework that allows you to configure where files are, how classes are named, etc.  By having a set of standard practices that you know your system follows, you can concentrate on what you&#8217;re doing and get sh$t done!  This is probably the <em>most</em> important tenant of Rails, and is pretty much what the entire thing is built on.</li>
<li><strong>Framework</strong> &#8211; Going back a bit, what the heck <em>is</em> a <a href="http://en.wikipedia.org/wiki/Web_application_framework">framework</a>.  Short answer is it&#8217;s something you build your applications in which provides you with everything you need.  In other words, instead of you having to deal with HTTP POST parameters for string processing, it provides you with libraries for data handling and common functionality.</li>
<li><strong>CRUD</strong> &#8211; Speaking of things you don&#8217;t have to deal with, the biggest thing that I think web app developers have to deal with is the four common actions you&#8217;re taking with data (think editing a list of blog posts): <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete"><strong>C</strong>reate, <strong>R</strong>etrieve, <strong>U</strong>pdate, and <strong>D</strong>elete</a>.  This is shorthand for telling people that the data you&#8217;re dealing with has (or needs) the standard set of actions to edit, delete, show and create functionality.  This goes hand in hand with our last one&#8230;</li>
<li><strong>Scaffolding</strong> &#8211; This is the concept of creating a default set of functionality that the programmer can update later on.  This is a quick and dirty way of getting things working, and is, in most cases, either only used for learning purposes or is rewritten later.  Creating a scaffold for a set of data (say a blog post) can be done in rails by running &#8220;script/generate scaffold Post title:string body:text&#8221;.  This will create a <a href="http://en.wikipedia.org/wiki/Scaffold_%28programming%29">scaffolding</a> which lets you have access to the CRUD operations on this new data model.  With just that one line above you get all the HTML, form processing, and database layer interaction needed to fully perform CRUD operations.  The concept of using scaffolding to create quick and easy access to data is a huge part of Rails&#8217; success (even if you <em>do</em> end up rewriting them eventually, or grow out of using scaffolding as your skills improve).</li>
</ul>
<p>There are <em>lots</em> more out there (<a href="http://www.brianburridge.com/2007/08/09/rails-terms-and-concepts/">this page</a> has a great list).  Phusion, Heroku, nginx, nokogiri, sass, haml, copilot and scout are other terms for some specific software and sites and libraries you might come across.</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkinginrails.com/2010/04/ruby-on-rails-lingo-for-beginners/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Quick Tip &#8211; Go Back and Re-Watch Things You Didn&#8217;t Understand Before</title>
		<link>http://thinkinginrails.com/2010/04/quick-tip-go-back-and-re-watch-things-you-didnt-understand-before/</link>
		<comments>http://thinkinginrails.com/2010/04/quick-tip-go-back-and-re-watch-things-you-didnt-understand-before/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 02:56:06 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://thinkinginrails.com/?p=293</guid>
		<description><![CDATA[Today I finally got around to watching the first Rails Dispatch episode of building a blog under Rails 3, and was surprised by a couple of things. First was that Rails 3 isn&#8217;t that different than Rails 2 (which BTW, is apparently obselete at least for the &#8220;normal&#8221; tasks of CRUD like operations in the [...]]]></description>
			<content:encoded><![CDATA[<p>Today I <em>finally</em> got around to watching the first <a href="http://railsdispatch.com/">Rails Dispatch</a> episode of <a href="http://railsdispatch.com/posts/rails-3-makes-life-better">building a blog under Rails 3</a>, and was surprised by a couple of things.</p>
<p>First was that Rails 3 isn&#8217;t <strong>that</strong> different than Rails 2 (which BTW, is <a href="http://5by5.tv/rubyshow/114">apparently obselete</a> <img src='http://thinkinginrails.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  at least for the &#8220;normal&#8221; tasks of CRUD like operations in the demo application.  A few bits of different syntax here and there, but for the most part it was all pretty recognizable.</p>
<p>I was secondly surprised at how much I understood.  Not that I&#8217;m dumb of course, but a lot of things in rails I just let wash over me and accept that I Just Don&#8217;t Get Yet<sup><small>(tm)(r)(c)</small></sup>.  This stuff however, was perfectly recognizable and grokkable, and because of that I actually picked up a couple of new things.</p>
<p>So my personal revelation of the day (I&#8217;m not going to call them tips cause that&#8217;d indicate that other people don&#8217;t all know this stuff already) is to go back and re-watch, re-read, or re-listen to things that you maybe didn&#8217;t fully grasp, or didn&#8217;t grasp that fully, and see if the second viewing with a few weeks or months of time gives you a better grasp of it.</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkinginrails.com/2010/04/quick-tip-go-back-and-re-watch-things-you-didnt-understand-before/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Enums For Constant Data in Rails and ActiveRecord</title>
		<link>http://thinkinginrails.com/2010/04/using-enums-for-constants/</link>
		<comments>http://thinkinginrails.com/2010/04/using-enums-for-constants/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 06:54:13 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://thinkinginrails.com/?p=261</guid>
		<description><![CDATA[I&#8217;ve been wondering what to write up for todays entry, in an effort to keep up the &#8220;post a day&#8221; discipline.   I have about eight drafts sitting in the wordpress queue that are either incomplete thoughts or completely uninteresting posts.  So I figured I&#8217;d take a look at what my next challenge is in my [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been wondering what to write up for todays entry, in an effort to keep up the &#8220;post a day&#8221; discipline.   I have about eight drafts sitting in the wordpress queue that are either incomplete thoughts or completely uninteresting posts.  So I figured I&#8217;d take a look at what <em>my</em> next challenge is in my own project.  After all, that&#8217;s what this site is for in some ways&#8230; a muse to get my off my butt and back into Rails code.</p>
<p>The last request that came in was for the ability to take a game object with a non-boolean, but discreet bit of data to specify if it is a boys only, girls only, or coed game.  A constant you might say.</p>
<p>Back in the old perl world I&#8217;d probably look at doing something like adding an integer field to the database and then just remember that 1 = boys, 2 = girls and 0 = coed.  Or something.  Or was it 3 = coed?  You see the problem here though.  Having to remember the settings, and make sure that all the different files output the same values for the same settings&#8230; ugh.  Even putting an output filter of some sort (or in rails parlance, a helper), is a bit of an ugly situation because that doesn&#8217;t help you with your database layer either.  I might also have used the &#8220;Constant&#8221; or &#8220;ReadOnly&#8221;modules to do the same, or even gone with the more complex solution is to create a foreign key into another table called &#8220;gamegendertype&#8221; or something that just has the 1 = boys, 2 = girls, 3 = coded, but that&#8217;s unneeded complexity, database lookups/joins, etc (though it doesmake it easier to add another data type).</p>
<p>So I dug into it to find out what the canonical &#8220;rails way&#8221; was to do it.  And that search led to <a href="http://en.wikipedia.org/wiki/Enumerated_type">enums</a>.  Enums allow you to specify your own discreet data types that can be searched on as if they were integers or the like, but are words like &#8220;Diamonds&#8221; &#8220;Hearts&#8221; &#8220;Clubs&#8221; and &#8220;Spades&#8221; for a solitaire game.</p>
<p>Rails doesn&#8217;t seem to have a built in way to do it, but the way that the various links I found had you do it were to store the string value in the backend database, but turn that into a :symbol for when it&#8217;s accessed to and from the front end.  A link is worth a thousand words though <img src='http://thinkinginrails.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<ul>
<li><a href="http://stackoverflow.com/questions/265725/what-is-the-best-way-to-handle-constants-in-ruby-when-using-rails">Using constants in RoR</a>, or how to <a href="http://stackoverflow.com/questions/693928/how-replacecreate-an-enum-field-on-rails-2-0-migrations">use them with migrations</a>, from StackOverflow</li>
<li><a href="http://www.snowgiraffe.com/tech/311/enumeration-columns-with-rails/">Enumeration columns</a> in rails</li>
<li>An (older) <a href="http://scripts.top4download.com/enum-column-plugin-for-rails/wefvo.html">Enum plugin</a></li>
<li><a href="http://zargony.com/2008/04/28/five-tips-for-developing-rails-applications">ActiveRecord enums</a> (#3 in the list, via the StackOverflow link)</li>
</ul>
<p>For my project I&#8217;d rather not use a plugin (trying to learn by doing and all) so I&#8217;ll probably go with the virtual attribute method (last link).  Keep an eye on the commit log on my <a href="http://github.com/arcterex">github account</a> to see where it goes <img src='http://thinkinginrails.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://thinkinginrails.com/2010/04/using-enums-for-constants/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

