<?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"
	>

<channel>
	<title>Three till Seven &#187; Ruby</title>
	<atom:link href="http://www.3till7.net/category/techy/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3till7.net</link>
	<description>A geek's personal domain.</description>
	<pubDate>Wed, 27 Aug 2008 22:19:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>chmodding and Ruby</title>
		<link>http://www.3till7.net/2008/02/01/chmodding-and-ruby/</link>
		<comments>http://www.3till7.net/2008/02/01/chmodding-and-ruby/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 20:52:03 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[OS X]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2008/02/01/chmodding-and-ruby/</guid>
		<description><![CDATA[Recently, I switched from a Powerbook to a Macbook, and to copy my files from one to the other, I used a pen drive.  Since my pen drive has a FAT file system, it treats everything as being executable.  This, however, is not the case on a UNIX-like file system like OS X. [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I switched from a Powerbook to a Macbook, and to copy my files from one to the other, I used a pen drive.  Since my pen drive has a FAT file system, it treats everything as being executable.  This, however, is not the case on a UNIX-like file system like OS X.  In order to save myself the hassle of manually chmodding thousands of files, I wrote this Ruby script:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b73615468f3">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> 'find'<br />
<span style="color:#CC0066; font-weight:bold;">require</span> 'fileutils'</p>
<p><span style="color:#008000; font-style:italic;"># Append to/remove from this list as necessary</span><br />
Plain = <span style="color:#006600; font-weight:bold;">&#91;</span>'php', 'txt', 'jpg', 'jpeg', 'gif', 'png', 'html', 'pdf', 'css', 'mp3', 'zip', 'tar.<span style="color:#9900CC;">gz</span>', 'tar', 'htm', 'psd', 'ai', 'ptg', 'cgi.<span style="color:#9900CC;">pl</span>', 'sphp', 'svn-base', 'default'<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> |ext|<br />
&nbsp; &nbsp; <span style="color:#996600;">&quot;.#{ext}&quot;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span>.<span style="color:#9900CC;">freeze</span></p>
<p><span style="color:#008000; font-style:italic;"># Append to/remove from this list as necessary</span><br />
Executable = <span style="color:#006600; font-weight:bold;">&#91;</span>'pl', 'rb', 'cgi', 'sh'<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> |ext|<br />
&nbsp; &nbsp; <span style="color:#996600;">&quot;.#{ext}&quot;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span>.<span style="color:#9900CC;">freeze</span><br />
$num_chmodded = <span style="color:#006666;">0</span></p>
<p><span style="color:#9966CC; font-weight:bold;">def</span> chmod_and_puts<span style="color:#006600; font-weight:bold;">&#40;</span> cmd <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> cmd<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">system</span> cmd<br />
&nbsp; &nbsp; $num_chmodded += <span style="color:#006666;">1</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></p>
<p><span style="color:#9966CC; font-weight:bold;">def</span> chmodder<span style="color:#006600; font-weight:bold;">&#40;</span> start_dir <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; num_skipped = <span style="color:#006666;">0</span><br />
&nbsp; &nbsp; num_plain = <span style="color:#006666;">0</span><br />
&nbsp; &nbsp; num_executable = <span style="color:#006666;">0</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Find.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span> start_dir <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |path|<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">unless</span> File.<span style="color:#9900CC;">symlink</span>?<span style="color:#006600; font-weight:bold;">&#40;</span> path <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> FileTest.<span style="color:#9900CC;">file</span>? path<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> is_plain? path<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chmod_and_puts <span style="color:#996600;">&quot;chmod 644 <span style="color:#000099;">\&quot;</span>#{path}<span style="color:#000099;">\&quot;</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_plain += <span style="color:#006666;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">elsif</span> is_executable? path<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chmod_and_puts <span style="color:#996600;">&quot;chmod 755 <span style="color:#000099;">\&quot;</span>#{path}<span style="color:#000099;">\&quot;</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_executable += <span style="color:#006666;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_skipped += <span style="color:#006666;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">elsif</span> File.<span style="color:#9900CC;">directory</span>? path<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chmod_and_puts <span style="color:#996600;">&quot;chmod 755 <span style="color:#000099;">\&quot;</span>#{path}<span style="color:#000099;">\&quot;</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_executable += <span style="color:#006666;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_skipped += <span style="color:#006666;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_skipped += <span style="color:#006666;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;----------------------------------------------&quot;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Chmodded #{$num_chmodded} total:&quot;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\t</span>Executable: #{num_executable}&quot;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\t</span>Plain: #{num_plain}&quot;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Skipped #{num_skipped} files/directories/symlinks&quot;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></p>
<p><span style="color:#9966CC; font-weight:bold;">def</span> get_extension<span style="color:#006600; font-weight:bold;">&#40;</span> path <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; File.<span style="color:#9900CC;">extname</span><span style="color:#006600; font-weight:bold;">&#40;</span> path <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">downcase</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></p>
<p><span style="color:#9966CC; font-weight:bold;">def</span> is_executable?<span style="color:#006600; font-weight:bold;">&#40;</span> path <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; extension = get_extension<span style="color:#006600; font-weight:bold;">&#40;</span> path <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> Executable.<span style="color:#9966CC; font-weight:bold;">include</span>? extension<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">true</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">false</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></p>
<p><span style="color:#9966CC; font-weight:bold;">def</span> is_plain?<span style="color:#006600; font-weight:bold;">&#40;</span> path <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; extension = get_extension<span style="color:#006600; font-weight:bold;">&#40;</span> path <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> Plain.<span style="color:#9966CC; font-weight:bold;">include</span>? extension<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">true</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">false</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></p>
<p>chmodder<span style="color:#006600; font-weight:bold;">&#40;</span> '.' <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
<p>This goes through all the files and directories within the directory from which you run the script and chmods non-executable files to 644 (read+write on User, read on Group and Other) and executable files to 755 (read+write+execute on User, read+execute on Group and Other).  If you're unfamiliar with the chmod command, you might want to read my <a href="/2005/12/21/beginning-linux-guide/?PHPSESSID=83d50da64c31f44557bdd3a90a3ae8db">beginning Linux guide</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2008/02/01/chmodding-and-ruby/feed/</wfw:commentRss>
		</item>
		<item>
		<title>using Ruby to rename files and edit their content</title>
		<link>http://www.3till7.net/2007/11/21/using-ruby-to-rename-files-and-edit-their-content/</link>
		<comments>http://www.3till7.net/2007/11/21/using-ruby-to-rename-files-and-edit-their-content/#comments</comments>
		<pubDate>Thu, 22 Nov 2007 03:22:10 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2007/11/21/using-ruby-to-rename-files-and-edit-their-content/</guid>
		<description><![CDATA[Recently at work, the web admin for the computer science department came into our lab and told us that my employer's site was broken.  The admin had need to make all .php files not act as PHP scripts, and instead, all files with the extension .sphp would now run as PHP scripts.  Since [...]]]></description>
			<content:encoded><![CDATA[<p>Recently at work, the web admin for the computer science department came into our lab and told us that my employer's site was broken.  The admin had need to make all .php files not act as PHP scripts, and instead, all files with the extension .<em>s</em>php would now run as PHP scripts.  Since my employer's site was built using PHP, that meant all of its pages were showing the source code instead of actually executing.  I had to whip up a quick Ruby script in order to:</p>
<ol>
<li>Rename all .php files to have .sphp as their extension;</li>
<li>Replace all instances of ".php" within those files with ".sphp", to take care of <code>include</code> statements, links, etc.</li>
</ol>
<p>Here's what I came up with:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b7361591527">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> 'find'<br />
<span style="color:#CC0066; font-weight:bold;">require</span> 'fileutils'</p>
<p><span style="color:#008000; font-style:italic;"># Used to go through, starting at the directory start_dir and working</span><br />
<span style="color:#008000; font-style:italic;"># recursively, and rename all files that end in .php to end in .sphp</span><br />
<span style="color:#008000; font-style:italic;"># because the CS admin got a wild hair.&nbsp; Also goes through all .php</span><br />
<span style="color:#008000; font-style:italic;"># and .sphp files and replaces all instances of &quot;.php&quot; in them with</span><br />
<span style="color:#008000; font-style:italic;"># &quot;.sphp&quot;.</span><br />
<span style="color:#9966CC; font-weight:bold;">def</span> finder<span style="color:#006600; font-weight:bold;">&#40;</span> start_dir <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; Find.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span> start_dir <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |path|<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> FileTest.<span style="color:#9900CC;">file</span>?<span style="color:#006600; font-weight:bold;">&#40;</span> path <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> path =~ /\.<span style="color:#9900CC;">php</span>$/i<br />
&nbsp; &nbsp; &nbsp; &nbsp; old_name = File.<span style="color:#9900CC;">basename</span><span style="color:#006600; font-weight:bold;">&#40;</span> path <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; new_name = old_name.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span> /\.<span style="color:#9900CC;">php</span>$/, '.<span style="color:#9900CC;">sphp</span>' <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dir = path.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span> /<span style="color:#008000; font-style:italic;">#{old_name}$/, '' )</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> File.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span> dir + old_name <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{dir + old_name} to #{dir + new_name}<span style="color:#000099;">\n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Since the code is part of a Subversion repository, we use</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># the 'svn' command to let Subversion rename the file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">system</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;svn mv #{dir + old_name} #{dir + new_name}&quot;</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">elsif</span> path =~ /\.<span style="color:#9900CC;">sphp</span>$/i || path =~ /\.<span style="color:#9900CC;">php</span>$/i<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Replacing instances of '.php' in #{path}<span style="color:#000099;">\n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">system</span><span style="color:#006600; font-weight:bold;">&#40;</span> %Q<span style="color:#006600; font-weight:bold;">&#123;</span>ruby -pe '<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/\\.<span style="color:#9900CC;">php</span>/, <span style="color:#996600;">&quot;.sphp&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>' -i <span style="color:#008000; font-style:italic;">#{path} } )</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></p>
<p>finder<span style="color:#006600; font-weight:bold;">&#40;</span> '.' <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2007/11/21/using-ruby-to-rename-files-and-edit-their-content/feed/</wfw:commentRss>
		</item>
		<item>
		<title>finding invalid foreign keys in Rails</title>
		<link>http://www.3till7.net/2007/11/15/finding-invalid-foreign-keys-in-rails/</link>
		<comments>http://www.3till7.net/2007/11/15/finding-invalid-foreign-keys-in-rails/#comments</comments>
		<pubDate>Thu, 15 Nov 2007 18:59:25 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2007/11/15/finding-invalid-foreign-keys-in-rails/</guid>
		<description><![CDATA[Sometimes it would be useful to tell users of your Ruby on Rails application if there is a problem in the database, such as some foreign keys are invalid.  As an example, let's assume you have two models, Book and Author, such that each Book has an author_id which connects with Author via its [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it would be useful to tell users of your Ruby on Rails application if there is a problem in the database, such as some foreign keys are invalid.  As an example, let's assume you have two models, Book and Author, such that each Book has an author_id which connects with Author via its primary key, id.  That is, the tables are:  books(id, author_id) and authors(id).  Each table probably has fields other than that, but those are the only fields we need to worry about.  Below is a method that generates an unordered HTML list for display to the users:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b73615da7c8">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Book &lt; ActiveRecord::Base<br />
&nbsp; <span style="color:#008000; font-style:italic;"># Returns HTML about which rows have invalid foreign keys</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_bad_foreigners</span><br />
&nbsp; &nbsp; msg = ''</p>
<p>&nbsp; &nbsp; find<span style="color:#006600; font-weight:bold;">&#40;</span> :all <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |book|<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">unless</span> Author.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span> book.<span style="color:#9900CC;">author_id</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; author_id = <span style="color:#006600; font-weight:bold;">&#40;</span> book.<span style="color:#9900CC;">author_id</span> || 'NULL' <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; msg &lt;&lt; '&lt;li&gt;Book with ID ' &lt;&lt; book.<span style="color:#9900CC;">id</span>.<span style="color:#9900CC;">to_s</span> + ' has a non-existent<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; author ID: ' &lt;&lt; author_id + '&lt;/li&gt;'<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> msg.<span style="color:#9900CC;">blank</span>?<br />
&nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">nil</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; '&lt;ul <span style="color:#9966CC; font-weight:bold;">class</span>=<span style="color:#996600;">&quot;error&quot;</span>&gt;' &lt;&lt; msg + '&lt;/ul&gt;' <br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div>
</div>
<p>You could have similar methods in your Rails models.  Maybe on the main page of your application, the above method could be displayed.  If there are no invalid foreign keys, nothing will show up.  Otherwise, the user will see a list of the invalid keys that need fixing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2007/11/15/finding-invalid-foreign-keys-in-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>the many methods to #find things in Rails</title>
		<link>http://www.3till7.net/2006/11/22/the-many-methods-to-find-things-in-rails/</link>
		<comments>http://www.3till7.net/2006/11/22/the-many-methods-to-find-things-in-rails/#comments</comments>
		<pubDate>Thu, 23 Nov 2006 00:52:50 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2006/11/22/the-many-methods-to-find-things-in-rails/</guid>
		<description><![CDATA[For the longest time, I didn&#8217;t understand the full power of the various #find methods in Rails.  I probably still don&#8217;t, but my view of them has certainly expanded.  I used to use plain ol&#8217; #find for everything.  If I wanted to find all rows in the table &#8216;groups&#8217; that had an [...]]]></description>
			<content:encoded><![CDATA[<p>For the longest time, I didn&#8217;t understand the full power of the various #find methods in Rails.  I probably still don&#8217;t, but my view of them has certainly expanded.  I used to use plain ol&#8217; #find for everything.  If I wanted to find all rows in the table &#8216;groups&#8217; that had an &#8216;id&#8217; field value of 1, 2, 3, or 4, I would do something like this:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b736161cb14">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;">Group.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; :all,<br />
&nbsp; :conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;id=? OR id=? OR id=? OR ID=?&quot;</span>, <span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
<p><!--mizore--><br />
Now, while this is a perfectly valid way to find those rows, it&#8217;s a little too&#8230;  verbose.  There&#8217;s a cleaner, shorter way to get the same result:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b7361626c81">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;">Group.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
<p>This works because #find can take any of the following as primary ID&#8217;s to look up:</p>
<ul>
<li>a single ID:
<div class="synthi_code" style="display:block;" id="styled_synthi_48b736162c34e">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;">Group.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</li>
<li>an array of ID&#8217;s:
<div class="synthi_code" style="display:block;" id="styled_synthi_48b7361633874">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;">Group.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</li>
<li>an expanded array of ID&#8217;s:
<div class="synthi_code" style="display:block;" id="styled_synthi_48b7361638f42">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;">Group.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span> *<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</li>
<li>multiple ID&#8217;s:
<div class="synthi_code" style="display:block;" id="styled_synthi_48b73616436fd">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;">Group.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">4</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</li>
</ul>
<p>Beyond the regular #find method, there are also auto-generated methods for each of your Rails models.  Say you have a UserGroup model that associates a user to a group (i.e. group membership).  The table &#8216;users_groups&#8217; has the fields &#8216;id&#8217;, &#8216;user_id&#8217;, and &#8216;group_id&#8217;.  What&#8217;s an easy way to find all users belonging to the group with ID #3?</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b7361646b9e">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;">UserGroup.<span style="color:#9900CC;">find_all_by_group_id</span><span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; <span style="color:#006666;">3</span>,<br />
&nbsp; :<span style="color:#9966CC; font-weight:bold;">include</span> =&gt; :user,<br />
&nbsp; :order =&gt; &#8216;users.<span style="color:#9900CC;">name</span> ASC&#8217;<br />
<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span> &amp;:user <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
<p>With that, you&#8217;re telling the UserGroup model to find all rows based on the group ID, which you give it as the first parameter.  You&#8217;re also telling it to eagerly load the associated rows from the &#8216;users&#8217; table, ordering the results by the user&#8217;s name.  Then, you&#8217;re using #map on the returned array to grab just the results of each row&#8217;s #user method.  Thus, you end up with an array of User objects, all of whom belong to the group with ID #3.</p>
<p>If you just want to 1) find a single row and 2) have the returned value be that row, and not an array, use a #find_by_FIELD_NAME method, instead of a #find_all_by_FIELD_NAME:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b736164d84f">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;">UserGroup.<span style="color:#9900CC;">find_by_user_id</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2006/11/22/the-many-methods-to-find-things-in-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>#post method in tests with a different controller</title>
		<link>http://www.3till7.net/2006/11/14/post-method-in-tests-with-a-different-controller/</link>
		<comments>http://www.3till7.net/2006/11/14/post-method-in-tests-with-a-different-controller/#comments</comments>
		<pubDate>Tue, 14 Nov 2006 23:46:54 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2006/11/14/post-method-in-tests-with-a-different-controller/</guid>
		<description><![CDATA[I wanted a #login method in test_helper that would allow me to easily login from any of my functional tests. However, the #post method won&#8217;t allow you to set a different controller than the one in the @controller instance variable that&#8217;s defined in your test&#8217;s #setup. Well, by looking at how the #process method works, [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted a #login method in test_helper that would allow me to easily login from any of my functional tests. However, the #post method won&#8217;t allow you to set a different controller than the one in the @controller instance variable that&#8217;s defined in your test&#8217;s #setup. Well, by looking at how the <a href="http://api.rubyonrails.org/classes/ActionController/TestProcess.html#M000046">#process method</a> works, you can see that it just grabs the controller from @controller. Redefine that, and you&#8217;re good to go:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b7361675710">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;">old_controller = @controller<br />
@controller = LoginController.<span style="color:#9900CC;">new</span><br />
post<span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; :attempt_login,<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span>:user =&gt; <span style="color:#006600; font-weight:bold;">&#123;</span>:name =&gt; &#8216;joe&#8217;, :password =&gt; &#8216;password&#8217;<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#41;</span><br />
@controller = old_controller</div>
</div>
<p><!--mizore--><br />
If you have several login methods, such as a #login_admin and #login_regular, you could make a wrapper to reduce duplication:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b736167a698">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> wrap_with_controller<span style="color:#006600; font-weight:bold;">&#40;</span> new_controller = LoginController <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; old_controller = @controller<br />
&nbsp; @controller = new_controller.<span style="color:#9900CC;">new</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">yield</span><br />
&nbsp; @controller = old_controller<br />
<span style="color:#9966CC; font-weight:bold;">end</span></p>
<p><span style="color:#9966CC; font-weight:bold;">def</span> login_admin<br />
&nbsp; wrap_with_controller <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; post<span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; :attempt_login,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span>:user =&gt; <span style="color:#006600; font-weight:bold;">&#123;</span>:name =&gt; &#8216;root&#8217;, :password =&gt; &#8216;password&#8217;<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></p>
<p><span style="color:#9966CC; font-weight:bold;">def</span> login_regular<br />
&nbsp; wrap_with_controller <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; post<span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; :attempt_login,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span>:user =&gt; <span style="color:#006600; font-weight:bold;">&#123;</span>:name =&gt; &#8216;joe&#8217;, :password =&gt; &#8216;password&#8217;<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div>
</div>
<p><em>Cross-posted on <a href="http://www.bigbold.com/snippets/user/moneypenny">my BigBold account</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2006/11/14/post-method-in-tests-with-a-different-controller/feed/</wfw:commentRss>
		</item>
		<item>
		<title>go from model to associated table name and back</title>
		<link>http://www.3till7.net/2006/08/31/go-from-model-to-associated-table-name-and-back/</link>
		<comments>http://www.3till7.net/2006/08/31/go-from-model-to-associated-table-name-and-back/#comments</comments>
		<pubDate>Thu, 31 Aug 2006 22:04:36 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2006/08/31/go-from-model-to-associated-table-name-and-back/</guid>
		<description><![CDATA[Given a table object, it returns the related string object; e.g. SubAttribute => &#8217;sub-attribute&#8217;. Useful if you want to make a list of all your tables with perhaps their fields listed out to the side.

 Ruby
def stringify_table&#40; table, replace_char = &#8216;-&#8217;, pluralize = false &#41;
&#160; string = table.to_s.gsub&#40; /&#40;&#91;A-Za-z&#93;&#41;&#40;&#91;A-Z&#93;&#41;/, &#8216;\1&#8216; &#60;&#60; replace_char.to_s &#60;&#60; &#8216;\2&#8216; &#41;
&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Given a table object, it returns the related string object; e.g. SubAttribute => &#8217;sub-attribute&#8217;. Useful if you want to make a list of all your tables with perhaps their fields listed out to the side.</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b73616c3918">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> stringify_table<span style="color:#006600; font-weight:bold;">&#40;</span> table, replace_char = &#8216;-&#8217;, pluralize = <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#CC0066; font-weight:bold;">string</span> = table.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span> /<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>A-Za-z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>A-Z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>/, &#8216;\<span style="color:#006666;">1</span>&#8216; &lt;&lt; replace_char.<span style="color:#9900CC;">to_s</span> &lt;&lt; &#8216;\<span style="color:#006666;">2</span>&#8216; <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#9900CC;">pluralize</span> <span style="color:#9966CC; font-weight:bold;">if</span> pluralize<br />
&nbsp; <span style="color:#CC0066; font-weight:bold;">string</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div>
</div>
<p><!--mizore--><br />
Given a string akin to the name of a table, it returns the related table object; e.g. &#8217;sub_attributes&#8217; => SubAttribute.</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b73616c7f6f">
<h2 class="synthi_header"> Ruby</h2>
<div class="ruby" style="font-family: monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> tablify_string<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#CC0066; font-weight:bold;">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span> /_id/, &#8221; <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">singularize</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span> &#8216;_&#8217; <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |word| word.<span style="color:#9900CC;">capitalize</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">join</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div>
</div>
<p>This entry was cross-posted to <a href="http://www.bigbold.com/snippets/user/moneypenny">my Code Snippets page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2006/08/31/go-from-model-to-associated-table-name-and-back/feed/</wfw:commentRss>
		</item>
		<item>
		<title>conditioner for ActiveRecord-friendly conditions from a collection</title>
		<link>http://www.3till7.net/2006/08/31/conditioner-for-activerecord-friendly-conditions-from-a-collection/</link>
		<comments>http://www.3till7.net/2006/08/31/conditioner-for-activerecord-friendly-conditions-from-a-collection/#comments</comments>
		<pubDate>Thu, 31 Aug 2006 21:55:24 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2006/08/31/conditioner-for-activerecord-friendly-conditions-from-a-collection/</guid>
		<description><![CDATA[I frequently have a collection of values that I want to match in an ActiveRecord query, but it would be nice if I could let ActiveRecord handle checking the data and escaping it properly. So, I wrote this method to return ActiveRecord-friendly conditions, such as:
["user_id=? AND job_id=?", 3, 4]
based on the &#8216;raw&#8217; conditions you feed [...]]]></description>
			<content:encoded><![CDATA[<p>I frequently have a collection of values that I want to match in an ActiveRecord query, but it would be nice if I could let ActiveRecord handle checking the data and escaping it properly. So, I wrote this method to return ActiveRecord-friendly conditions, such as:<br />
<code>["user_id=? AND job_id=?", 3, 4]</code><br />
based on the &#8216;raw&#8217; conditions you feed to it, such as:<br />
<code>[['user_id', 3], ['job_id', 4]]</code><br />
<!--mizore--><br />
<code># Returns ActiveRecord-friendly conditions based on the given<br />
# raw conditions; handles grouping based on like field names;<br />
# allows different boolean operators in raw conditions;<br />
# allows different comparison operators in raw conditions;<br />
# raw conditions setup:<br />
# [[field name, desired value, bool. op., comp. op.], &#8230;]<br />
# raw conditions example:<br />
# [['type_id', '4', 'OR'], ['created_on', Date.new, 'AND', '&lt;=']]<br />
def conditioner( raw_conditions )<br />
&nbsp;&nbsp;conditions = ["("]<br />
&nbsp;&nbsp;count = 0<br />
&nbsp;&nbsp;prev_name = raw_conditions[0][0]<br />
&nbsp;&nbsp;raw_conditions.each do |condition|<br />
&nbsp;&nbsp;&nbsp;&nbsp;name = condition[0]<br />
&nbsp;&nbsp;&nbsp;&nbsp;value = condition[1]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;if condition[2]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool_type = condition[2]<br />
&nbsp;&nbsp;&nbsp;&nbsp;else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool_type = &#8216;OR&#8217;<br />
&nbsp;&nbsp;&nbsp;&nbsp;end<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;if condition[3]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comparison = condition[3]<br />
&nbsp;&nbsp;&nbsp;&nbsp;else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comparison = &#8216;=&#8217;<br />
&nbsp;&nbsp;&nbsp;&nbsp;end<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;conditions[0] &lt;&lt; &#8216;) AND &#8216; if prev_name != name<br />
&nbsp;&nbsp;&nbsp;&nbsp;conditions[0] &lt;&lt; &#8216; &#8216; &lt;&lt; bool_type.to_s &lt;&lt; &#8216; &#8216; unless count == 0 || prev_name != name<br />
&nbsp;&nbsp;&nbsp;&nbsp;conditions[0] &lt;&lt; &#8216;(&#8217; if prev_name != name<br />
&nbsp;&nbsp;&nbsp;&nbsp;conditions[0] &lt;&lt; &#8220;#{name} #{comparison} ?&#8221;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;conditions &lt;&lt; value<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;prev_name = name<br />
&nbsp;&nbsp;&nbsp;&nbsp;count += 1<br />
&nbsp;&nbsp;end<br />
&nbsp;&nbsp;conditions[0] &lt;&lt; &#8216;)&#8217;</p>
<p>&nbsp;&nbsp;conditions<br />
end</code></p>
<p>This way, you can do something like the following:<br />
<code>model_ids = Model.find( :all ).map( &#038;:id )<br />
raw_conditions = model_ids.collect { |id| ['model_id', id] }<br />
conditions = conditioner( raw_conditions )<br />
desired_collection = OtherModel.find( :all, :conditions => conditions )</code></p>
<p>This entry was cross-posted to <a href="http://www.bigbold.com/snippets/user/moneypenny">my Code Snippets page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2006/08/31/conditioner-for-activerecord-friendly-conditions-from-a-collection/feed/</wfw:commentRss>
		</item>
		<item>
		<title>simple Rails preference storage</title>
		<link>http://www.3till7.net/2006/08/11/simple-rails-preference-storage/</link>
		<comments>http://www.3till7.net/2006/08/11/simple-rails-preference-storage/#comments</comments>
		<pubDate>Sat, 12 Aug 2006 00:01:29 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2006/08/11/simple-rails-tips-for-preference-storage/</guid>
		<description><![CDATA[So you&#8217;ve got some Rails application and you need to store information from the users across their interactions with the app.  Here&#8217;s a simple, straightforward way to do that.

In your controller:
if params[:option]
&#160;&#160;@option = session[:option] = params[:option]
elsif session[:option]
&#160;&#160;@option = session[:option]
else
&#160;&#160;@option = &#8216;default value&#8217;
end
This checks to see if &#8216;option&#8217; was passed via a parameter in the [...]]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;ve got some Rails application and you need to store information from the users across their interactions with the app.  Here&#8217;s a simple, straightforward way to do that.<br />
<!--mizore--><br />
In your controller:</p>
<blockquote><p><code>if params[:option]<br />
&nbsp;&nbsp;@option = session[:option] = params[:option]<br />
elsif session[:option]<br />
&nbsp;&nbsp;@option = session[:option]<br />
else<br />
&nbsp;&nbsp;@option = &#8216;default value&#8217;<br />
end</code></p></blockquote>
<p>This checks to see if &#8216;option&#8217; was passed via a parameter in the URI (e.g. /view?option=jim) or from a form.  If it is, then we want to save that option in both the session, so we can access it later, as well as in a variable specifically for use in our view.</p>
<p>However, if <code>params[:option]</code> is nil but we still have it in session[:option] from some previous interaction, then we&#8217;ll set our variable <code>@option</code> for use in our view from the session copy.</p>
<p>If all else fails, then we know the user hasn&#8217;t tried to set this option in any way, and we can give it some default value.</p>
<p>Now, in our view:</p>
<blockquote><p><code>&lt;%= form_tag :action => 'view' %&gt;<br />
&lt;label for=&#8221;option&#8221;&gt;Choose:&lt;/label&gt;<br />
&lt;%= text_field_tag &#8216;option&#8217;, @option, :size =&gt; 10 %&gt;<br />
&lt;%= submit_tag &#8216;Change&#8217; %&gt;<br />
&lt;/form&gt;<br />
</code></p></blockquote>
<p>This provides a simple form for our user to set the value of this option, and if the value is already set from a previous interaction with this form, then it shows up in the text field.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2006/08/11/simple-rails-preference-storage/feed/</wfw:commentRss>
		</item>
		<item>
		<title>if succinctly</title>
		<link>http://www.3till7.net/2006/06/07/if-succinctly/</link>
		<comments>http://www.3till7.net/2006/06/07/if-succinctly/#comments</comments>
		<pubDate>Thu, 08 Jun 2006 02:21:54 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2006/06/07/if-succinctly/</guid>
		<description><![CDATA[In Ruby, if is an expression, not a statement, thus it returns a value.  This may not seem useful at first glance, but it lends itself to forming neat, concise code&#8230;  Like most things in Ruby, actually.

Setting Values Concisely
Say you want to define a variable differently based on the result of an if [...]]]></description>
			<content:encoded><![CDATA[<p>In Ruby, <code>if</code> is an expression, not a statement, thus it returns a value.  This may not seem useful at first glance, but it lends itself to forming neat, concise code&#8230;  Like most things in Ruby, actually.<br />
<!--mizore--></p>
<h2>Setting Values Concisely</h2>
<p>Say you want to define a variable differently based on the result of an <code>if</code> expression.  If you&#8217;re not a native to Ruby, you may find yourself coding something like this:</p>
<p><code>if name == 'Susie'</code><br />
<code>&nbsp;&nbsp;age = 12</code><br />
<code>else</code><br />
<code>&nbsp;&nbsp;age = 14</code><br />
<code>end</code></p>
<p>This breaks from the <acronym title="Don't Repeat Yourself">DRY</acronym> principle because <code>age =</code> is repeated in each case.  Since <code>if</code> is an expression, however, you could write something like this:</p>
<p><code>age = if name == 'susie'</code><br />
<code>&nbsp;&nbsp;12</code><br />
<code>else</code><br />
<code>&nbsp;&nbsp;14</code><br />
<code>end</code></p>
<p>This evaluates the condition <code>name == 'susie'</code> and if it&#8217;s true, dumps the value 12 into <code>age</code>; oppositely, if it&#8217;s false, it dumps the value 14 into <code>age</code>.  To be even more succinct about it, try:</p>
<p><code>age = name == 'susie' ? 12 : 14</code></p>
<h2>One-Line <code>if</code>&#8217;s</h2>
<p>A lot of the time it may be necessary to write out a one-line block of code within an <code>if</code>.  There are several ways to do this without being as verbose as</p>
<p><code>if <em>expression</em></code><br />
<code>&nbsp;&nbsp;<em>action</em></code><br />
<code>end</code></p>
<p>Three lines!  How wasteful.  Instead, try one of the following:</p>
<ol>
<li><code>if <em>expression</em> then <em>action</em></code></p>
<p>or more concisely:</li>
<li><code>if <em>expression</em>: <em>action</em></code>
<p>or my personal favorite:</li>
<li><code><em>action</em> if <em>expression</em></code>
<p>or for the C fan:</li>
<li><code><em>expression</em> ? <em>if result is true</em> : <em>if result is false</em></code></li>
</ol>
<p>For a negated <code>if</code> (i.e. <code>if !<em>expression</em></code>), it may make more sense to you to use <code>unless</code>:</p>
<p><code>unless <em>expression</em></code><br />
<code>&nbsp;&nbsp;<em>action</em></code><br />
<code>end</code></p>
<p>You can use <code>unless</code> in the same way that you use <code>if</code>:  all the one-liners above will work.  Note, however, that <code>elsif</code> is not available with an <code>unless</code> expression, though <code>else</code> is.</p>
<h2>Doubling Up</h2>
<p>You can have expressions all over the place, following and preceding blocks.  For example:</p>
<p><code>while <em>condition</em></code><br />
<code>&nbsp;&nbsp;<em>action</em></code><br />
<code>end if <em>expression</em></code></p>
<p>That <code>while</code> loop will only get executed if <code><em>expression</em></code> is true.  This may not be the most readable chunk of code, however, since the <code>while</code> is a multi-line block.  However, <code>if</code> and <code>unless</code> expressions following one-line blocks are handy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2006/06/07/if-succinctly/feed/</wfw:commentRss>
		</item>
		<item>
		<title>the power of yield</title>
		<link>http://www.3till7.net/2006/05/28/the-power-of-yield/</link>
		<comments>http://www.3till7.net/2006/05/28/the-power-of-yield/#comments</comments>
		<pubDate>Mon, 29 May 2006 01:51:05 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2006/05/28/the-power-of-yield/</guid>
		<description><![CDATA[&#8230;and super.  These two keywords allow you to pass control back and forth between parent and child methods, to weave power between a more general method (in the parent class) and a more specific method (in the child class) with ease and logic.  Using yield and super effectively can help you maintain the [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230;and <code>super</code>.  These two keywords allow you to pass control back and forth between parent and child methods, to weave power between a more general method (in the parent class) and a more specific method (in the child class) with ease and logic.  Using <code>yield</code> and <code>super</code> effectively can help you maintain the DRY (<strong>D</strong>on&#8217;t <strong>r</strong>epeat <strong>y</strong>ourself) principle, keeping your code easier to maintain.<br />
<!--mizore--><br />
Say you have three classes for image creation:  a parent class, <code>Image</code>, and two children:  <code>Raster</code> and <code>Vector</code>.  Now for your raster images, you&#8217;re going to be using the <a href="http://www.boutell.com/gd/">GD2</a> library <a href="http://gd2.rubyforge.org/">for Ruby</a>, and for your vector images, you&#8217;re going to be creating <a href="http://www.w3.org/Graphics/SVG/">SVG</a> images.  Some of the functionality used to create any given image will be different, naturally, since with the raster images, you&#8217;re creating an image object, and with the vector images, you&#8217;re concatenating strings of XML SVG code.  However, a lot of the code will be the same, and should thus be shared between the two children to avoid duplication.  For example, the logic used to determine what color some text should be, or what shape you should draw, can be shared between the two child classes.  Any shared code should go in the one common class between the two children:  the parent class, <code>Image</code>.</p>
<h2>Accessing Shared Code</h2>
<p>That is, accessing code in a parent class method from a child class method of the same name.  The trick is with <code><a href="http://en.wikibooks.org/wiki/Programming:Ruby_Method_Calling">super</a></code>.</p>
<h3><code>write_text()</code> in <code>Raster</code></h3>
<blockquote><p><code>def write_text( text, color )</code><br />
<code class="attention">&nbsp;&nbsp;super() do |width, height, left, top|</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;image = Image.new( width, height )</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;image.draw do |canvas|</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.color = color</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.font = Font::TrueType['/usr/share/fonts/times.ttf', 20]</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.move_to( left, top )</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.text( text )</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;end</code><br />
<code>&nbsp;&nbsp;end</code></p>
<p><code>&nbsp;&nbsp;image</code><br />
<code>end</code></p></blockquote>
<h3><code>write_text()</code> in <code>Vector</code></h3>
<blockquote><p><code>def write_text( text, color )</code><br />
<code>&nbsp;&nbsp;image = '&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;'</code><br />
<code>&nbsp;&nbsp;image << '&lt;!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd"&gt;'</code></p>
<p><code class="attention">&nbsp;&nbsp;super() do |width, height, left, top|</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;image << '&lt;svg viewBox="0 0 ' + width.to_s + ' ' + height.to_s + '" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"&gt;'</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;image << '&lt;text font-family="Times" x="' + left.to_s + '" y="' + top.to_s + '" fill="' + color.to_s + '"&gt;'</code><br />
<code>&nbsp;&nbsp;end</code></p>
<p><code>&nbsp;&nbsp;image << text.to_s</code><br />
<code>&nbsp;&nbsp;image << '&lt;/text&gt;'</code><br />
<code>&nbsp;&nbsp;image << '&lt;/svg&gt;'</code></p>
<p><code>&nbsp;&nbsp;image</code><br />
<code>end</code></p></blockquote>
<p>While these two methods accomplish the same thing, they do it differently.  However, each of them has a call to <code>super</code>.  That will access a method of the same name in the parent class:</p>
<h3><code>write_text()</code> in <code>Image</code></h3>
<blockquote><p><code>def write_text</code><br />
<code class="attention">&nbsp;&nbsp;yield( 300, 100, 5, 5 )</code><br />
<code>end</code></p></blockquote>
<p>All that <code>Image</code>&#8217;s <code>write_text()</code> is doing is passing values back to the child method that called it.  Each child method&#8217;s <code>super</code> call defines variables in the block: width, height, left, and top in the line that says <code>super() do |width, height, left, top|</code>.  The <code>yield</code> call in <code>Image</code> defines those values to be width = 300, height = 100, left = 5, and top = 5.  To <code>Image</code>&#8217;s <code>write_text()</code>, however, it&#8217;s just throwing some numbers at the child method:  it doesn&#8217;t know that the child method is going to use those numbers to define the dimensions of the image and the starting position of its text.</p>
<p>The definition of <code>write_text()</code> in <code>Image</code> is very simple, but it doesn&#8217;t have to be that way.  There could be a great deal of calculation involved in determining what values the child methods should use for whichever variables are being passed to it.  The parent class&#8217;s shared method is a place to store shared logic and shared data; let the child classes do the specific stuff with that general data.</p>
<h2>Shared Variables <em>Sans</em> <code>yield</code></h2>
<p>Values can also be reached in the child class methods without using <code>yield</code> if instance variables are defined in the parent method:</p>
<h3><code>write_text()</code> in <code>Image</code></h3>
<blockquote><p><code>def write_text</code><br />
<code>&nbsp;&nbsp;@width = 300</code><br />
<code>&nbsp;&nbsp;@height = 100</code><br />
<code>&nbsp;&nbsp;@left = @top = 5</code><br />
<code>end</code></p></blockquote>
<p>Then <code>write_text()</code> in both <code>Raster</code> and <code>Vector</code> would be able to access <code>Image</code>&#8217;s <code>@width</code>, <code>@height</code>, <code>@left</code>, and <code>@top</code>, as defined in its <code>write_text()</code>.</p>
<h2>Multiple Calls to <code>super</code></h2>
<p>Sometimes it may be useful to let the parent class&#8217;s method do some work, then the child class&#8217;s, then the parent&#8217;s again.  In this case, you can call <code>super</code> and <code>yield</code> as many times as necessary:</p>
<h3>Method in Child Class</h3>
<blockquote><p><code>def my_method</code><br />
<code>&nbsp;&nbsp;return1 = super( 'a string' ) do |my_var|</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;# Do some stuff with my_var</code><br />
<code>&nbsp;&nbsp;end</code></p>
<p><code>&nbsp;&nbsp;return2 = super( ['array', 'of', 'strings'] ) do |my_other_var|</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;# Do some stuff with my_other_var</code><br />
<code>&nbsp;&nbsp;end</code><br />
<code>end</code></p></blockquote>
<h3>Method in Parent Class</h3>
<blockquote><p><code>def my_method( value )</code><br />
<code>&nbsp;&nbsp;if value.class == String</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;yield( 'this is my_var' )</code><br />
<code>&nbsp;&nbsp;elsif value.class == Array</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;yield( 'this is my_other_var' )</code><br />
<code>&nbsp;&nbsp;end</code></p>
<p><code>&nbsp;&nbsp;# Checks to see if value can work with the &lt;&lt;</code><br />
<code>&nbsp;&nbsp;# method, which both Array and String objects</code><br />
<code>&nbsp;&nbsp;# have, and if so, adds another string to it;</code><br />
<code>&nbsp;&nbsp;# this value is returned since it's the last</code><br />
<code>&nbsp;&nbsp;# line of the method</code><br />
<code>&nbsp;&nbsp;value &lt;&lt; ' appended to value' if value.respond_to?( :&lt;&lt; )</code><br />
<code>end</code></p></blockquote>
<h2><code>yield</code> Returns Data to <code>super</code></h2>
<p>The child class method can also throw data back to the parent class method by returning a value from the <code>super</code> block:</p>
<h3>Method in Child Class</h3>
<blockquote><p><code>def my_method</code><br />
<code>&nbsp;&nbsp;value_from_parent = super do |my_var|</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;'this gets thrown back to the parent class method'</code><br />
<code>&nbsp;&nbsp;end</code><br />
<code>end</code></p></blockquote>
<h3>Method in Parent Class</h3>
<blockquote><p><code>def my_method</code><br />
<code>&nbsp;&nbsp;value_from_child = yield( 'becomes my_var' )</code><br />
<code>&nbsp;&nbsp;'this gets thrown back to the child class method'</code><br />
<code>end</code></p></blockquote>
<h2>More Information</h2>
<ul>
<li><a href="http://www.rubycentral.com/book/tut_classes.html">Programming Ruby: Classes, Objects, and Variables</a> - Examples of <code>super</code> and <code>yield</code> use.</li>
<li><a href="http://www.artima.com/intv/dry.html">Orthogonality and the DRY Principle</a> - &ldquo;DRY says that every piece of system knowledge should have one authoritative, unambiguous representation.&rdquo;</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2006/05/28/the-power-of-yield/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
