<?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>Cylence, Inc. &#187; Ruby on Rails</title>
	<atom:link href="http://blog.cylence.com/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cylence.com</link>
	<description>Elegance in Technology</description>
	<lastBuildDate>Wed, 16 Mar 2011 20:16:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A Few of My Favorite Things: scope and Symbol#to_proc</title>
		<link>http://blog.cylence.com/2011/03/16/scope-and-symbol-to_proc/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=scope-and-symbol-to_proc</link>
		<comments>http://blog.cylence.com/2011/03/16/scope-and-symbol-to_proc/#comments</comments>
		<pubDate>Wed, 16 Mar 2011 07:22:34 +0000</pubDate>
		<dc:creator>Ryan Cross</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.cylence.com/?p=206</guid>
		<description><![CDATA[Let's say you have a User model. Let's also say that you want the ability to archive your user records. So, you add a boolean attribute called archived that defaults to false which can be set to true in order to archive your user records. Now you can look for archived records by performing the [...]]]></description>
			<content:encoded><![CDATA[<p>Let's say you have a User model. Let's also say that you want the ability to archive your user records. </p>
<p>So, you add a boolean attribute called <code>archived</code> that defaults to <code>false</code> which can be set to <code>true</code> in order to archive your user records. Now you can look for archived records by performing the following query throughout your app:</p>
<p><script src="https://gist.github.com/872136.js?file=scope.rb"></script></p>
<p>Well that's nifty&#8230; until you realize that you're having to write out that blasted <code>where()</code> statement in five billion places throughout your application. Is that DRY? Quite the opposite, I tell you! What happens if you change that attribute's name, you're bound to miss at least a few instances of it in the change. For the love of all that is holy &#8211; this has just become a horrible solution!</p>
<p>Four solutions to this problem spring to mind: one is outright dumb, two are common and one is simply beautiful.</p>
<p>The first option is to put a query like this in our views.</p>
<p><script src="https://gist.github.com/873217.js?file=gistfile1.rhtml"></script></p>
<p>Seriously, if you find yourself putting ActiveRecord queries in your views, just go back to PHP, please. If you're simply new to Rails and honestly don't know any better, read on. The second option is to put this query in your controller.</p>
<p><script src="https://gist.github.com/872136.js?file=gistfile3.rb"></script></p>
<p>While this isn't necessarily a bad solution, it tends to get repeated&#8230; a lot in some cases, which once again goes against the "Don't Repeat Yourself" mantra. Another solution is to move this logic into the model. Well, well. You're one smart cookie, you! Skinny controllers, fat models. Let's move this clunky query into the model!</p>
<p><script src="https://gist.github.com/872138.js?file=model_method.rb"></script></p>
<p>This is much prettier! But this method is gonna get all kinds of lost in any respectably sized model. This used to be the best way to store a query for frequent use until Rails 2.1 was released with named_scope in its bag of tricks. Now, in Rails 3, <code>named_scope</code> has a new persona, and it's here to&#8230; fight crime&#8230; and stuff.</p>
<h3>Enter scope</h3>
<p>The <code>scope</code> method belongs in the list of incredibly useful model methods along with associations and validations. This method expects two arguments; either a name and a condition or a name and a block. We can easily convert our above query using the name and condition combination.</p>
<p><script src="https://gist.github.com/872136.js?file=gistfile5.rb"></script></p>
<p>This can be used to grab all of the archived users, similar to the previous example.</p>
<p><script src="https://gist.github.com/872136.js?file=gistfile6.rb"></script></p>
<p>Delightful! Seriously, I feel like I just ate a Hot Pocket. Oh, actually, that doesn't feel good at all. But this scope looks great! It's clean, on one line and behaves beautifully.</p>
<p>So, how would we use a block with scope? Think of any situation where you'd want to pass data into a query. This is where you'd use a block with scope. In our example, we want to return all users with whatever role we pass it.</p>
<p><script src="https://gist.github.com/872136.js?file=gistfile7.rb"></script></p>
<p>Now we reference the scope, passing a role into it as an option.</p>
<p><script src="https://gist.github.com/872136.js?file=gistfile8.rb"></script></p>
<p>Holy cow! So, basically we can take a massive query and slap a small scope name onto it and reference it whenever and wherever we need to. If this query changes, we change it on one place. Graceful, elegant and easy to update.</p>
<p>Now that you know how scopes work, I would be a horrible teacher if I didn't introduce you to <code>default_scope</code>. This beast will allow you to specify some query goodness that you want appended to *every* query you run on that model. Using our example above, what good would an <code>archived</code> scope be if <code>User.all</code> returned the archived users too! Not very much use in my book. We can use <code>default_scope</code> to tell the model when we search normally, we only want the users who are not archived to be returned.</p>
<p><script src="https://gist.github.com/872136.js?file=gistfile9.rb"></script></p>
<p>Now when we do a search for users, it will only ever return users who have archived set to false. Slick! But what if we want to get around that for a query or two? Let's say we want to count ALL users, archived or not. We'd use <code>unscoped</code> here. Let's pretend we have 75 users, 5 of which have archived set to true</p>
<p><script src="https://gist.github.com/872136.js?file=gistfile2.rb"></script></p>
<h3>Symbol#to_proc</h3>
<p>One more thing. You may have recognized a reoccurring bit of code throughout my examples.</p>
<p><script src="https://gist.github.com/872138.js?file=symbol_to_proc.rb"></script></p>
<p>We're iterating through the list of users in the @users instance variable and outputting the name attribute of each. If you only need one attribute from each record in a list, you have just encountered one use case for Symbol#to_proc. Instead of an <code>each</code> block, we could simply use the following:</p>
<p><script src="https://gist.github.com/872138.js?file=gistfile1.rb"></script></p>
<p>This list of names can now be used however you like. Perhaps a nice join() method would meet the need?</p>
<p><script src="https://gist.github.com/872138.js?file=gistfile2.rb"></script></p>
<p>Now, go call your mom and tell her all about the wonderful things you learned here. After that, go forth and use scopes and Symbol#to_proc like the mofo you know you are!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cylence.com/2011/03/16/scope-and-symbol-to_proc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Listing Social Networks in Ruby</title>
		<link>http://blog.cylence.com/2010/06/25/listing-social-networks-in-ruby/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=listing-social-networks-in-ruby</link>
		<comments>http://blog.cylence.com/2010/06/25/listing-social-networks-in-ruby/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 06:03:04 +0000</pubDate>
		<dc:creator>Ryan Cross</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://blog.cylence.com/?p=150</guid>
		<description><![CDATA[I spent a little time tonight working on The Enclave's new member page. The site overall is the most beautiful thing in the world, but I have been using it as an excuse to plan more with Sinatra. Tonight, I found the need to provide a way for our members to list their social networks [...]]]></description>
			<content:encoded><![CDATA[<p>I spent a little time tonight working on <a href="http://enclavecoop.com">The Enclave's</a> new <a href="http://enclavecoop.com/members">member page</a>.  The site overall is the most beautiful thing in the world, but I have been using it as an excuse to plan more with <a href="http://www.sinatrarb.com/">Sinatra</a>.</p>
<p>Tonight, I found the need to provide a way for our members to list their social networks should they choose to do so.  The obvious way was to add a list of anchor tags.  I found this to be rather distasteful.  I mean, we are dealing with Ruby here, right?  So, I started by considering what I wanted the UI code to look like.  Would do I want to have to work with on a weekly basis.  I came up with the following:</p>
<p><script src="http://gist.github.com/453831.js"></script></p>
<p>This would allow me to add as many usernames and networks as I desired.  Perfect!  So I began to work on the code that would make this function as intended.  I came up with the following:</p>
<p><script src="http://gist.github.com/453827.js"></script></p>
<p>So, basically, I list all of the networks I want to use.  This list not only includes the URL (and a place holder for the username), but any prefix or suffix for the displayed username for which I might find a need (i.e.: Twitter's "@" in "@slant")  Next, the script iterates through my list, catching each network as it does so.  Each time the script reaches a network, it proceeds through each username in the list, building an appropriate anchor tag for each one.</p>
<p><script src="http://gist.github.com/453837.js"></script></p>
<p>In the end, you have a beautiful categorized list of usernames, each linking to their respective page.</p>
<div class="wp_syntax">
<div class="code">linkedin: <a href="http://www.linkedin.com/in/ryanlcross">ryanlcross</a> | facebook: <a href="http://www.facebook.com/rcross">rcross</a> | twitter: <a href="http://twitter.com/slant">@slant</a>, <a href="http://twitter.com/cylenceweb">@cylenceweb</a></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.cylence.com/2010/06/25/listing-social-networks-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Journey to Snow Leopard</title>
		<link>http://blog.cylence.com/2009/09/07/journey-to-snow-leopard/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=journey-to-snow-leopard</link>
		<comments>http://blog.cylence.com/2009/09/07/journey-to-snow-leopard/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 19:44:17 +0000</pubDate>
		<dc:creator>Ryan Cross</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[snowleopard]]></category>

		<guid isPermaLink="false">http://blog.cylence.com/?p=103</guid>
		<description><![CDATA[Like many, I was in my local Apple Store on August 28th to pick up their newest operating system update, Snow Leopard. Despite hearing about some compatibility issues, my inner-first-adopter got the better of me and since it was basically the beginning of a weekend, I began down the path of the upgrade. Luckily, I [...]]]></description>
			<content:encoded><![CDATA[<p>Like many, I was in my local Apple Store on August 28th to pick up their newest operating system update, <a href="http://www.apple.com/macosx/">Snow Leopard</a>. Despite hearing about some compatibility issues, my inner-first-adopter got the better of me and since it was basically the beginning of a weekend, I began down the path of the upgrade.</p>
<p>Luckily, I walked out of the installation process unscathed for the most part with only a few minor issues to deal with.  One example would be that <a href="http://www.modrails.com/">Phusion Passenger</a> was no longer working &#8211; but mongrel was &#8211; so that was livable.  Realizing that I'd not yet setup a decent database for development since getting my new MacBook Pro, I set to work on basically reinstalling the entire Rails stack to ensure that everything would be ready to roll when I get back to work on Monday.</p>
<p>I found a few fantastic resources to guide me through the majority of what I was looking to do including <a href="http://hivelogic.com/">Hivelogic's</a> guide on <a href="http://hivelogic.com/articles/compiling-ruby-rubygems-and-rails-on-snow-leopard/">Compiling Ruby, RubyGems, and Rails on Snow Leopard</a> as well as another guide on <a href="http://hivelogic.com/articles/compiling-git-on-snow-leopard/">Compiling Git on Snow Leopard</a>.  I also found some great information on <a href="http://www.hypertopic.org/index.php/Step-by-step_Install_Guide_for_Leopard">installing PostgreSQL on Snow Leopard</a>. Lastly, I discovered a very <a href="http://www.icoretech.org/2009/08/how-to-install-pg-postgresql-gem-on-snow-leopard-64-bit/">short guide</a> on compiling the PostgreSQL RubyGem for use on Snow Leopard (enabling 64bit compatibility).</p>
<p>Equipped with these guides, I successfully got everything upgraded and am now waiting for all of the other 10.6 bugs to pop up!</p>
<p>edit: A <a href="http://afreshcup.com/2009/09/02/migrating-to-snow-leopard-for-rails-development-a-definitive-guide/">definitive guide</a> in deed. I just found a guide for those of you who want to install things like MySQL and SQLite as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cylence.com/2009/09/07/journey-to-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Presentation at Refresh Denver</title>
		<link>http://blog.cylence.com/2008/09/22/presentation-at-refresh-denver/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=presentation-at-refresh-denver</link>
		<comments>http://blog.cylence.com/2008/09/22/presentation-at-refresh-denver/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 19:14:35 +0000</pubDate>
		<dc:creator>Ryan Cross</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[refreshdenver]]></category>

		<guid isPermaLink="false">http://blog.cylenceweb.com/?p=76</guid>
		<description><![CDATA[Last week, Jay Graves and myself presented the differences between Ruby on Rails and Django at Refresh Denver.  We decided on a common database schema, functionality and design.  We then each built the same application in our respective frameworks. During the presentation, we each explained our frameworks on a surface level followed by a brief [...]]]></description>
			<content:encoded><![CDATA[<p>Last week, <a href="http://jay.skabber.com/">Jay Graves</a> and myself <a href="http://refreshdenver.org/2008/09/09/september-17-battle-royale-django-vs-rails/">presented</a> the differences between <a href="http://rubyonrails.org/">Ruby on Rails</a> and <a href="http://www.djangoproject.com/">Django</a> at <a href="http://www.refreshdenver.com/">Refresh Denver</a>.  We decided on a common database schema, functionality and design.  We then each built the same application in our respective frameworks.</p>
<p>During the presentation, we each explained our frameworks on a surface level followed by a brief demonstration of how each framework accomplished a few of the things that our applications did.</p>
<p>Jay's presentation can be found <a href="http://www.slideshare.net/skabber/introduction-to-django-presentation">here</a> and his Django application <a href="http://github.com/skabber/spamstr-django/">here</a>.</p>
<p>My presentation can be found <a href="http://www.slideshare.net/slant/overview-of-ruby-on-rails-presentation/">here</a> and my Rails application <a href="http://github.com/cylence/spamstr-rails/">here</a>.</p>
<p>Overall, I absolutely believed this presentation to be a success!  I'd like to see other frameworks and languages go head to head like this in the future.  I feel like the ability to see two frameworks side by side was a real benefit, allowing a clearer look at how each works with respect to the other.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cylence.com/2008/09/22/presentation-at-refresh-denver/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using Hash#values</title>
		<link>http://blog.cylence.com/2008/07/28/using-hashvalues/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-hashvalues</link>
		<comments>http://blog.cylence.com/2008/07/28/using-hashvalues/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 22:54:31 +0000</pubDate>
		<dc:creator>Ryan Cross</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[hash]]></category>

		<guid isPermaLink="false">http://blog.cylenceweb.com/?p=13</guid>
		<description><![CDATA[This is minor, but I thought it was interesting nonetheless. I needed to iterate through the values of a Hash. So I called .values on the Hash in question. What was interesting about this had nothing to do with the results of calling this method, but instead what it was actually doing to the Hash. [...]]]></description>
			<content:encoded><![CDATA[<p>This is minor, but I thought it was interesting nonetheless.</p>
<p>I needed to iterate through the values of a Hash.  So I called <code>.values</code> on the Hash in question.  What was interesting about this had nothing to do with the results of calling this method, but instead what it was actually doing to the Hash.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> myhash = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:first</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;one&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:second</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;two&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:third</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;three&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:second<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;two&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:first</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;one&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:third</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;three&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The <code>myhash</code> how has a class of <code>Hash</code>, which is to be expected, since that's what I gave it.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> myhash.<span style="color:#9966CC; font-weight:bold;">class</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Hash</span></pre></div></div>

<p>When <code>.values</code> is called, it returns all of the values in the Hash.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> myhash.<span style="color:#9900CC;">values</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;one&quot;</span>, <span style="color:#996600;">&quot;two&quot;</span>, <span style="color:#996600;">&quot;three&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>You may have noticed this in that last example, but when <code>.values</code> was called on that Hash, the class of the result changed altogether.  Let's examine the class of this Hash while we are calling <code>.values</code> on it.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> myhash.<span style="color:#9900CC;">values</span>.<span style="color:#9966CC; font-weight:bold;">class</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">Array</span></pre></div></div>

<p>So calling <code>.values</code> on the Hash literally built an Array with the values from the preceding Hash!</p>
<p>Read more about <a href="http://www.ruby-doc.org/core/classes/Hash.html#M000178">Hash#values</a> in the <a href="http://www.ruby-doc.org/core/">Ruby Documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cylence.com/2008/07/28/using-hashvalues/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Git Aliases</title>
		<link>http://blog.cylence.com/2008/07/28/git-aliases/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=git-aliases</link>
		<comments>http://blog.cylence.com/2008/07/28/git-aliases/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 22:39:46 +0000</pubDate>
		<dc:creator>Ryan Cross</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[alias]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://blog.cylenceweb.com/?p=7</guid>
		<description><![CDATA[There has been talk about how to shorten Git commands, mostly in an effort to assist those of us who are coming from Subversion. One proposed solution has been to use the alias mechanism native to your shell environment. Another solution does exist. This one is internal to git itself. Due to that fact, the [...]]]></description>
			<content:encoded><![CDATA[<p>There has been talk about how to shorten Git commands, mostly in an effort to assist those of us who are coming from Subversion.  One proposed solution has been to use the alias mechanism native to your shell environment.</p>
<p>Another solution does exist.  This one is internal to git itself.  Due to that fact, the two main benefits are a shorter load time when starting a new shell (since you're not dealing with alias entries in your .profile script) and easier git-centric configuration.</p>
<p>Simply add the following to <code>~/.gitconfig</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[alias]
  co = checkout
  st = status
  br = branch
  ba = branch -a</pre></div></div>

<p>Now instead of typing <code>git checkout branch</code> you only need to type <code>git co branch</code>.  You can customized these as much as you'd like, adding other commands as well, as long as the command after the equals sign is a valid git command.</p>
<p>The obvious down side to this method is that you have to type <code>git</code> before these aliases.  Using the other method, you do not.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cylence.com/2008/07/28/git-aliases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

