<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:admin="http://webns.net/mvcb/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:content="http://purl.org/rss/1.0/modules/content/">

    <channel>
    
    <title>A Web Developer from Maine</title>
    <link>http://www.codeofficer.com/blog/</link>
    <description></description>
    <dc:language>en</dc:language>
    <dc:creator>spam@codeofficer.com</dc:creator>
    <dc:rights>Copyright 2010</dc:rights>
    <dc:date>2010-09-30T04:19:00-05:00</dc:date>
    <admin:generatorAgent rdf:resource="http://expressionengine.com/" />
    

    <item>
      <title>Easy environment specific seed files in Rails 3</title>
      <link>http://www.codeofficer.com/blog/entry/easy_environment_specific_seed_files_in_rails_3/</link>
      <guid>http://www.codeofficer.com/blog/entry/easy_environment_specific_seed_files_in_rails_3/#When:04:19:00Z</guid>
      <description><p>Tonight I discovered a <a href="https://rails.lighthouseapp.com/projects/8994/tickets/4908-feature-request-db-seed-files-for-each-environment">lighthouse ticket</a> by <a href="http://houseofpostmodern.wordpress.com/" title="House of Postmodern">Postmodern</a>. In it, he asks about adding an environment aware feature to the existing &#8216;rake db:seed&#8217; task, so that he could seed specific environments differently. He even includes <a href="http://github.com/postmodern/rails/commit/7dd0718239c6747e1a6981aed9b9c406532e9828" title="Commit 7dd0718239c6747e1a6981aed9b9c406532e9828 to postmodern's rails - GitHub">a patch</a>.
</p>
<p>
If you haven&#8217;t used rails &#8216;rake db:seed&#8217; functionality, its quite handy. I use it primarily during the pre-launch phase of development to fill out my app with dummy data, most of which is generated using the <a href="http://github.com/EmmanuelOga/ffaker" title="EmmanuelOga's ffaker at master - GitHub">ffaker</a> gem. That all works fine for me!
</p>
<p>
Up until ... the time that my client wants to see progress and I need to deploy my code to a staging server. For that I require an entirely different set of seed data. Try as I might, I just can&#8217;t get the client used to my beloved &#8220;Lorem ipsum dolor&#8221;. So, environment aware seeding is very useful to me. 
</p>
<p>
A neat thing about rake tasks is that when you redeclare a task, it doesnt overwrite the previous one. So, to use postmodern&#8217;s patch all you have to do is paste the following code to the bottom of your Rakefile and you&#8217;ll reap the full benefits.
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #0000BB">namespace&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">db&nbsp;</span><span style="color: #007700">do<br />&nbsp;&nbsp;</span><span style="color: #0000BB">task&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">seed&nbsp;</span><span style="color: #007700">=&gt;&nbsp;:</span><span style="color: #0000BB">environment&nbsp;</span><span style="color: #007700">do<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">env_seed_file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">File</span><span style="color: #007700">.</span><span style="color: #0000BB">join</span><span style="color: #007700">(</span><span style="color: #0000BB">Rails</span><span style="color: #007700">.</span><span style="color: #0000BB">root</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'db'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'seeds'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"#&#123;Rails.env&#125;.rb"</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">load</span><span style="color: #007700">(</span><span style="color: #0000BB">env_seed_file</span><span style="color: #007700">)&nbsp;if&nbsp;</span><span style="color: #0000BB">File</span><span style="color: #007700">.</span><span style="color: #0000BB">exist</span><span style="color: #007700">?(</span><span style="color: #0000BB">env_seed_file</span><span style="color: #007700">)<br />&nbsp;&nbsp;</span><span style="color: #0000BB">end<br />end</span>
</code></div><p>
</p>
<p>
Now you can do stuff like this! (the env specific file is loaded after the regular seeds file)
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #0000BB">db<br />├──&nbsp;seeds<br />│  &nbsp;├──&nbsp;development</span><span style="color: #007700">.</span><span style="color: #0000BB">rb<br />│  &nbsp;├──&nbsp;production</span><span style="color: #007700">.</span><span style="color: #0000BB">rb<br />│  &nbsp;└──&nbsp;staging</span><span style="color: #007700">.</span><span style="color: #0000BB">rb<br />└──&nbsp;seeds</span><span style="color: #007700">.</span><span style="color: #0000BB">rb</span>
</code></div><p>
</p>
<p>
Thanks Postmodern <img src="http://www.codeofficer.com/images/smileys/smile.gif" width="19" height="19" alt="smile" style="border:0;" />
</p></description>
      <dc:subject>Programming, Ruby on Rails</dc:subject>
      <dc:date>2010-09-30T04:19:00-05:00</dc:date>
    </item>

    <item>
      <title>Form builders in Rails: field names and ids for Javascript</title>
      <link>http://www.codeofficer.com/blog/entry/form_builders_in_rails_discovering_field_names_and_ids_for_javascript/</link>
      <guid>http://www.codeofficer.com/blog/entry/form_builders_in_rails_discovering_field_names_and_ids_for_javascript/#When:21:09:00Z</guid>
      <description><p>I came up with a few convenience methods today which will help you discover what Rail&#8217;s generated NAME and ID attributes will be for a specific field on a given form builder object. Actually, I didn&#8217;t come up with them so much as extract them from the built in form builder. It was useful enough to me that I thought I might share my discovery. I can&#8217;t tell you how many times I&#8217;ve lazily hardcoded an ID onto a form element just to bypass the defaults.
</p>
<p>
Throw these 2 methods in your ApplicationHelper module:
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #0000BB">def&nbsp;field_id_for_js</span><span style="color: #007700">(</span><span style="color: #0000BB">builder</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">attribute</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"#&#123;builder.object_name&#125;&#91;#&#123;attribute.to_s.sub(/\?$/,"")&#125;&#93;"</span><span style="color: #007700">.</span><span style="color: #0000BB">gsub</span><span style="color: #007700">(/</span><span style="color: #0000BB">\&#93;\&#91;</span><span style="color: #007700">|</span><span style="color: #0000BB">&#91;</span><span style="color: #007700">^-</span><span style="color: #0000BB">a</span><span style="color: #007700">-</span><span style="color: #0000BB">zA</span><span style="color: #007700">-</span><span style="color: #0000BB">Z0</span><span style="color: #007700">-</span><span style="color: #0000BB">9</span><span style="color: #007700">:.</span><span style="color: #0000BB">&#93;</span><span style="color: #007700">/,&nbsp;</span><span style="color: #DD0000">"_"</span><span style="color: #007700">).</span><span style="color: #0000BB">sub</span><span style="color: #007700">(/</span><span style="color: #0000BB">_</span><span style="color: #007700">$/,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">)<br /></span><span style="color: #0000BB">end<br /><br />def&nbsp;field_name_for_js</span><span style="color: #007700">(</span><span style="color: #0000BB">builder</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">attribute</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"#&#123;builder.object_name&#125;&#91;#&#123;attribute.to_s.sub(/\?$/,"")&#125;&#93;"<br /></span><span style="color: #0000BB">end</span>
</code></div><p>
</p>
<p>
Here is an example using Rails nested attribute forms:
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #007700">&lt;%=&nbsp;</span><span style="color: #0000BB">form_for</span><span style="color: #007700">(@</span><span style="color: #0000BB">submission</span><span style="color: #007700">)&nbsp;do&nbsp;|</span><span style="color: #0000BB">f</span><span style="color: #007700">|&nbsp;%&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;%=&nbsp;</span><span style="color: #0000BB">f</span><span style="color: #007700">.</span><span style="color: #0000BB">fields_for&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">project</span><span style="color: #007700">,&nbsp;@</span><span style="color: #0000BB">submission</span><span style="color: #007700">.</span><span style="color: #0000BB">project&nbsp;</span><span style="color: #007700">do&nbsp;|</span><span style="color: #0000BB">builder</span><span style="color: #007700">|&nbsp;%&gt;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">div&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"field"</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%=&nbsp;</span><span style="color: #0000BB">builder</span><span style="color: #007700">.</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">classified</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Classified?"&nbsp;</span><span style="color: #007700">%&gt;&lt;</span><span style="color: #0000BB">br</span><span style="color: #007700">/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%=&nbsp;</span><span style="color: #0000BB">builder</span><span style="color: #007700">.</span><span style="color: #0000BB">radio_button&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">classified</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">%&gt;&nbsp;&lt;%=&nbsp;</span><span style="color: #0000BB">builder</span><span style="color: #007700">.</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">classified_false</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"No"&nbsp;</span><span style="color: #007700">%&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%=&nbsp;</span><span style="color: #0000BB">builder</span><span style="color: #007700">.</span><span style="color: #0000BB">radio_button&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">classified</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">%&gt;&nbsp;&nbsp;&lt;%=&nbsp;</span><span style="color: #0000BB">builder</span><span style="color: #007700">.</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">classified_true</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Yes"&nbsp;</span><span style="color: #007700">%&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color: #0000BB">div</span><span style="color: #007700">&gt;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%=&nbsp;</span><span style="color: #0000BB">content_for</span><span style="color: #007700">(:</span><span style="color: #0000BB">head</span><span style="color: #007700">)&nbsp;do&nbsp;%&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">script</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(function()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;get&nbsp;our&nbsp;value&nbsp;by&nbsp;name<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">$(</span><span style="color: #DD0000">"input&#91;name='&lt;%=&nbsp;field_name_for_js(builder,&nbsp;:classified)&nbsp;%&gt;'&#93;"</span><span style="color: #007700">).</span><span style="color: #0000BB">val</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;set&nbsp;our&nbsp;value&nbsp;by&nbsp;ID<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">$(</span><span style="color: #DD0000">"#&lt;%=&nbsp;field_id_for_js(builder,&nbsp;:classified_true)&nbsp;%&gt;"</span><span style="color: #007700">).</span><span style="color: #0000BB">attr</span><span style="color: #007700">(</span><span style="color: #DD0000">'checked'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&lt;/script&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">&lt;%&nbsp;</span><span style="color: #0000BB">end&nbsp;</span><span style="color: #007700">%&gt;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;%&nbsp;</span><span style="color: #0000BB">end&nbsp;</span><span style="color: #007700">%&gt;<br />&lt;%&nbsp;</span><span style="color: #0000BB">end&nbsp;</span><span style="color: #007700">%&gt;</span>
</code></div><p>
</p>
<p>
Would output:
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #007700">&lt;</span><span style="color: #0000BB">script</span><span style="color: #007700">&gt;<br />$(function()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;get&nbsp;our&nbsp;value&nbsp;by&nbsp;name<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">$(</span><span style="color: #DD0000">"input&#91;name='submission&#91;project_attributes&#93;&#91;classified&#93;'&#93;"</span><span style="color: #007700">).</span><span style="color: #0000BB">val</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;set&nbsp;our&nbsp;value&nbsp;by&nbsp;ID<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">$(</span><span style="color: #DD0000">"#submission_project_attributes_classified_true"</span><span style="color: #007700">).</span><span style="color: #0000BB">attr</span><span style="color: #007700">(</span><span style="color: #DD0000">'checked'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">&#125;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">&lt;/script&gt;</span>
</code></div><p>
</p>
<p>
In case you missed it ...
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #007700">&lt;%=&nbsp;</span><span style="color: #0000BB">field_name_for_js</span><span style="color: #007700">(</span><span style="color: #0000BB">builder</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">classified</span><span style="color: #007700">)&nbsp;%&gt;<br />&lt;%=&nbsp;</span><span style="color: #0000BB">field_id_for_js</span><span style="color: #007700">(</span><span style="color: #0000BB">builder</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">classified_true</span><span style="color: #007700">)&nbsp;%&gt;</span>
</code></div><p>
</p>
<p>
... produced ...
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #0000BB">submission&#91;project_attributes&#93;&#91;classified&#93;<br />submission_project_attributes_classified_true</span>
</code></div><p>
	
<br />
Pretty handy stuff <img src="http://www.codeofficer.com/images/smileys/smile.gif" width="19" height="19" alt="smile" style="border:0;" />
</p></description>
      <dc:subject>Programming, Ruby on Rails</dc:subject>
      <dc:date>2010-09-27T21:09:00-05:00</dc:date>
    </item>

    <item>
      <title>jQuery helpers for Rails 3.0.0.beta</title>
      <link>http://www.codeofficer.com/blog/entry/jquery_helpers_for_rails_300beta/</link>
      <guid>http://www.codeofficer.com/blog/entry/jquery_helpers_for_rails_300beta/#When:17:58:00Z</guid>
      <description><p>I&#8217;ve just ported the unobtrusive javascript helpers in Rails 3 from Prototype to jQuery-1.4.1, but I haven&#8217;t tested it to work with earlier versions of jQuery yet. Let me know if you find this useful or have any issues installing the gem. 
</p>
<p>
For those that don&#8217;t know, Rails 3 now makes use of HTML5 custom data attributes on the DOM instead of generating javascript inline when using it&#8217;s built in javascript helpers. If you&#8217;re not planning to use HTML5 in your app, you might benefit from another plugin I wrote called <a href="http://github.com/CodeOfficer/js-data-helper" title="CodeOfficer's js-data-helper at master - GitHub">js-data-helper</a>.
</p>
<p>
Thanks to <a href="http://technicalpickles.com/" title="home at technical.pickles">@technicalpickles</a> for helping me package this up as a gem. 
</p>
<p>
The code is on Github.
<br />
<a href="http://github.com/CodeOfficer/jquery-helpers-for-rails3" title="jquery-helpers-for-rails3">http://github.com/CodeOfficer/jquery-helpers-for-rails3</a>
</p>
<p>
The gem is on Gemcutter
<br />
<a href="http://gemcutter.org/gems/jquery_helpers" title="jquery_helpers">http://gemcutter.org/gems/jquery_helpers</a>
</p>
<pre>
Install:
  (sudo) gem install jquery_helpers

Usage:
  rails generate jquery_helpers  [options]

Runtime options:
  -f, [--force]    # Overwrite files that already exist
  -p, [--pretend]  # Run but do not make any changes
  -q, [--quiet]    # Supress status output
  -s, [--skip]     # Skip files that already exist

Description:
    A port of the unobtrusive Prototype helpers in Rails 3 to jQuery

Example:
    ./script/generate jquery_helpers

    This will create:
        public/javascripts/jquery.rails.js
        public/javascripts/jquery.rails.min.js
</pre>
</p>
<p>
Don&#8217;t forget to include &#8216;jquery.rails.js&#8217; in your layout along with jQuery.
</p>
<p>
<b>UPDATE May 11, 2010:</b>
<br />
Rails has their own port to jQuery that is better maintained now. 
<br />
<a href="http://github.com/rails/jquery-ujs">http://github.com/rails/jquery-ujs</a>
<br />

</p></description>
      <dc:subject></dc:subject>
      <dc:date>2010-02-08T17:58:00-05:00</dc:date>
    </item>

    <item>
      <title>Intridea discovers a great tech community in Portland Maine</title>
      <link>http://www.codeofficer.com/blog/entry/intridea_discovers_a_flourishing_tech_community_in_portland_maine/</link>
      <guid>http://www.codeofficer.com/blog/entry/intridea_discovers_a_flourishing_tech_community_in_portland_maine/#When:02:25:00Z</guid>
      <description><p>This week I had the pleasure of attending <a href="http://intridea.com/hackon" title="Hackon">Hackon</a>, a FREE 3 day technology event thrown by <a href="http://intridea.com/" title="Intridea">Intridea</a> here in Portland, Maine. It took place at the <a href="http://www.portlandharborhotel.com/" title="Portland Harbor Hotel Downtown Portland">Portland Harbor Hotel</a> and was organized by <a href="http://www.adambair.com/" title="Adam Bair">Adam Bair</a>, a local friend, Ruby enthusiast and employee of Intridea. Adam did a wonderful Job of organizing the event. Many thanks to Intridea for making it all possible! 
</p>
<p>
The first 2 days of the event were mostly an open format. Guests brought their laptops and worked off the Hotel&#8217;s wireless. I honestly don&#8217;t think I &#8220;worked&#8221; very much that 1st day. Instead, I had a wonderful time greeting the attendees, many of whom I had not met before. Surprisingly not everyone was a developer but we had tons to talk about and Day 1 flew by without warning. Thankfully, I did manage to spend most of my time programming on Day 2.
</p>
<p>
I had a very interesting conversation with <a href="http://fettig.net/about/" title="About Abe Fettig">Abe Fettig</a>, a Google employee living here in town. He was nice enough to spend a couple hours live coding some Javascript at my request, giving me a great review of prototypal inheritance. I learned so much from our talk. That conversation was really something money couldn&#8217;t buy and I&#8217;m still soaking it in! Thanks Abe!
</p>
<p>
Throughout the event, I discussed with Abe and others a rather complex project I have been working on that makes heavy use of Javascript. I introduced quite a few of these people to <a href="http://code.quirkey.com/sammy/" title="Aaron Quint :: Web Developer :: Brooklyn, NY">Sammy</a>, a relatively new jQuery plugin I have decided to use in my project. I am a huge fan of this plugin! It allows you to define Javascript applications as objects attached to your DOM in such a way that makes complicated client side apps easier to manage. Its heavily based on Ruby&#8217;s <a href="http://www.sinatrarb.com/" title="Sinatra">Sinatra</a> gem and very easy to use.
</p>
<p>
Day 3 offered a number of very interesting presentations:
</p>
<ul>

<p>
<li><a href="http://justbeast.livejournal.com/" title="The Sarmatian Protopope">Dimitri</a> demonstrated a web framework written in <a href="http://en.wikipedia.org/wiki/Smalltalk" title="Smalltalk">Smalltalk</a> called <a href="http://www.seaside.st/" title="seaside.st">Seaside</a>. </li>
<li><a href="http://hackingsearch.com/" title="Ryan Smith">Ryan Smith</a> spoke on improving Apache configurations.</li>
<li><a href="http://www.tiffanyrad.com/" title="Tiffany Strauchs Rad">Tiffany Rad</a> and Nothingface spoke about a VERY cool open source hardware hacking project called <a href="https://secure.area49.net/openotto/" title="OpenOtto">OpenOtto</a>.</li>
<li><a href="http://inkfist.com/" title="InkFist.Com">Bogart Zalzberg</a> described a <a href="http://inkfist.com/postfix_talk/" title="Slides of /postfix_talk">humorous story</a> of a poorly configured Postfix installation.</li>
</ul>
<p>
I am anxious to see if Indridea will host Hackon again next year. I still can&#8217;t believe this was a free event. In Maine, its so difficult to find the variety and caliber of tech folks that were in attendance in one place. I imagine that <a href="http://techmaine.com/" title="TechMaine">TechMaine</a> would benefit from hosting similar events. 
</p>
<p>
And oohh look, I guess some <a href="http://gallery.me.com/codeofficer#100071" title="MobileMe Gallery">photos were taken</a> of the event!
</p></description>
      <dc:subject></dc:subject>
      <dc:date>2009-06-21T02:25:00-05:00</dc:date>
    </item>

    <item>
      <title>My Initial Impressions of Tweetie for Mac</title>
      <link>http://www.codeofficer.com/blog/entry/my_initial_impressions_of_tweetie_for_mac/</link>
      <guid>http://www.codeofficer.com/blog/entry/my_initial_impressions_of_tweetie_for_mac/#When:00:24:00Z</guid>
      <description><p><a href="http://www.atebits.com/tweetie-mac/" title="Tweetie for Mac">Tweetie for Mac</a> came out yesterday. I&#8217;ve anxiously awaited its release since I&#8217;m already such a fan of the ever popular <a href="http://www.atebits.com/tweetie-iphone/" title="Tweetie for iPhone">Tweetie for iPhone</a>. However, I was a bit underwhelmed with what it has to offer. Tweetie for Mac&#8217;s strongest feature is its sexy aesthetic quality, which just &#8220;looks&#8221; different. That feature alone can make it seem superior to other cloned desktop <a href="https://twitter.com/" title="Twitter">Twitter</a> clients, but I don&#8217;t think its enough to make it a truly great app. Not yet, and not without some additional effort. I&#8217;m waiting for some updates.
</p>
<p>
Some Initial complaints I have are:
</p>
<p>
<b>No Growl Integration</b>
</p>
<p>
Don&#8217;t force me to have my Twitter app in the foreground in order to see tweets as they come in. Most good mac applications already take advantage of growl integration. <a href="http://growl.info/" title="Growl">Growl</a> is a system level notification system that lets you customize how various applications get your attention. Lack of Growl integration is a deal breaker for me. I already have a ton of windows open for my web work and screen real-estate is a crucial commodity, even with dual 20 inch screens.
</p>
<p>
<b>No Compact Layout</b>
</p>
<p>
It would be nice if one could have the option of using a more compact layout. Resizing the main window, the smallest size you can achieve is 355x422 pixels. Considering you only see 3 to 4 tweets at this size, its very hard to keep Tweetie from dominating your screen space, and still be useful.
</p>
<p>
<b>No Saved Searches</b>
</p>
<p>
Tweetie for iPhone has this and its one of things I enjoy most about it. 
</p>
<p>
<b>No Find Nearby Tweets Feature</b>
</p>
<p>
I&#8217;m not sure if Apple&#8217;s CoreLocation framework is available for desktop use yet, but this is something the iPhone app offers and its just awesome. I have met some amazing people recently through Tweetie for iPhone. I&#8217;ve even met a few for coffee. Its wonderful to connect that way, and I&#8217;d love if my desktop Twitter client could serve the same purpose.
</p>
<p>
<b>Heavy API Useage</b>
</p>
<p>
I have found Tweetie to be a bit chatty. It seems to make a lot of calls back to Twitter&#8217;s API service and I&#8217;ve been locked out of using Twitter twice now. When that happens I think you have to wait an hour before you can receive tweets again. No good!
</p>
<p>
I&#8217;m currently using Twitteriffic on my desktop computers but I won&#8217;t say I&#8217;m particularly loyal to it. I&#8217;m mostly dependent on it&#8217;s Growl integration. I&#8217;ve tried TweetDeck and others, each have wonderful features but also their own issues. The Air apps often don&#8217;t feel like real mac applications. Thats annoying to me. I want the Twitter client I use to behave in a mac-like way. I don&#8217;t mind being shown ads for free use, and I&#8217;ll certainly pay for good software. Tweetie for Mac has a great aesthetic, it reminds me of <a href="http://culturedcode.com/things/" title="Things">Things</a>. I&#8217;m hopeful they will add some of the features I require to use it.
<br />

</p></description>
      <dc:subject>Programming, Mac OS X</dc:subject>
      <dc:date>2009-04-21T00:24:00-05:00</dc:date>
    </item>

    <item>
      <title>7 Best of Breed Rails Plugins I Can&#8217;t Live Without</title>
      <link>http://www.codeofficer.com/blog/entry/7_best_of_breed_rails_plugins_i_cant_live_without/</link>
      <guid>http://www.codeofficer.com/blog/entry/7_best_of_breed_rails_plugins_i_cant_live_without/#When:21:14:00Z</guid>
      <description><p>
I am often asked about the various gems/plugins I use in my Rails apps and thought it might make for a good blog post. Most of these I have a good history with, while some I settled on only recently. Either way, this list contains what I consider to be the best of the best, for what each of them helps to accomplish. I hope you will check them out if you have not yet had a chance to.
</p>
<p>
<strong>REVISIONING</strong>
</p>
<p>
<strong>acts_as_revisable</strong> (<a href="http://github.com/rich" title="rich's Profile - GitHub">rich</a>)
<br />
A framework for building heavily versioned applications.
<br />
<a href="http://github.com/rich/acts_as_revisable/tree/master" title="rich's acts_as_revisable at master - GitHub">http://github.com/rich/acts_as_revisable/</a>
</p>
<p>
acts_as_revisable is a joy to use. It doesn&#8217;t have you create separate tables for versions like some other popular versioning plugins. The interface is very well thought out, and I&#8217;ve yet to find something it doesn&#8217;t do that I&#8217;ve needed. Its heavily documented and just works!
</p>
<p>
<strong>TAGGING</strong>
</p>
<p>
<strong>acts-as-taggable-on</strong> (<a href="http://github.com/mbleigh" title="mbleigh's Profile - GitHub">mbleigh</a>)
<br />
A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
<br />
<a href="http://github.com/mbleigh/acts-as-taggable-on/tree/master" title="mbleigh's acts-as-taggable-on at master - GitHub">http://github.com/mbleigh/acts-as-taggable-on/</a>
</p>
<p>
A tried various tagging solutions over the last year and finally settled on this one by mbleigh. acts-as-taggable-on-steroids and a few others never seemed to work on models build with single table inheritance (STI) or that had polymorphic relationships.
</p>
<p>
<strong>AUTHENTICATION</strong>
</p>
<p>
<strong>authlogic</strong> (<a href="http://github.com/binarylogic" title="binarylogic's Profile - GitHub">binarylogic</a>)
<br />
A clean, simple, and unobtrusive ruby authentication solution.
<br />
<a href="http://github.com/binarylogic/authlogic/tree/master" title="binarylogic's authlogic at master - GitHub">http://github.com/binarylogic/authlogic/</a>
</p>
<p>
Like most of the Rails community, I earned my stripes using Restful Authentication, a sexy authentication generator that got you up and running in no time at all, but it wasn&#8217;t long after that I began to find things I didn&#8217;t like about it. Overall, Restful Authentication felt very non-object oriented. It polluted my user model with code I hadn&#8217;t written and it didn&#8217;t even feel restful. Authlogic on the other hand makes no assumptions on how you&#8217;d like to authenticate users. It lets you treat your UserSession as a model and take advantage of ActiveRecord&#8217;s build in callbacks. Its also a Gem and easy to update. Nowadays you won&#8217;t find me using anything else. Its also heavily documented.
</p>
<p>
<strong>BACKGROUND JOBS</strong>
</p>
<p>
<strong>delayed_job</strong> (<a href="http://github.com/tobi" title="tobi's Profile - GitHub">tobi</a>)
<br />
Database backed asynchronous priority queue&#8212;Extracted from Shopify.
<br />
<a href="http://github.com/tobi/delayed_job/tree/master" title="tobi's delayed_job at master - GitHub">http://github.com/tobi/delayed_job/</a>
</p>
<p>
I don&#8217;t have a lot of experience with background process plugins, I&#8217;ll admit that up front. Hands down though, this one has some slick features and is very actively developed. No complaints. 
</p>
<p>
<strong>GIT WORKFLOW</strong>
</p>
<p>
<strong>git_remote_branch</strong> (<a href="http://github.com/webmat" title="webmat's Profile - GitHub">webmat</a>)
<br />
A tool to simplify working with remote branches.
<br />
<a href="http://github.com/webmat/git_remote_branch/tree/master" title="webmat's git_remote_branch at master - GitHub">http://github.com/webmat/git_remote_branch/</a>
</p>
<p>
If you are a frequent brancher, and need to sync local branches with your remote server there is no better way than using git_remote_branch. You can publish and track local branches in a single command. I find myself doing this a lot when I have created a branch on my laptop but want to continue where I left off on my other machine at home. It also has a helpful &#8216;explain&#8217; feature to tell you exactly which commands will run should you use one of its features. 
</p>
<p>
<strong>GEM BUILDING</strong>
</p>
<p>
<strong>jeweler</strong> (<a href="http://github.com/technicalpickles" title="technicalpickles's Profile - GitHub">technicalpickles</a>)
<br />
Simple and opinionated helper for creating and managing Rubygem projects on GitHub.
<br />
<a href="http://github.com/technicalpickles/jeweler/tree/master" title="technicalpickles's jeweler at master - GitHub">http://github.com/technicalpickles/jeweler/</a>
</p>
<p>
I knew nothing about writing gems, and pushing them to github ... 20 minutes later I had. Nuff said.
</p>
<p>
<strong>STATE MACHINES</strong>
</p>
<p>
<strong>state_machine</strong> (<a href="http://github.com/pluginaweek" title="pluginaweek's Profile - GitHub">pluginaweek</a>)
<br />
Adds support for creating state machines for attributes on any Ruby class.
<br />
<a href="http://github.com/pluginaweek/state_machine/tree/master" title="pluginaweek's state_machine at master - GitHub">http://github.com/pluginaweek/state_machine/</a>
</p>
<p>
This one I found by a <a href="http://heypanda.com/" title="heypanda: A Guided Introduction to the Silliness of Mason's Mind">friend&#8217;s</a> recommendation. I had been using Rubyist&#8217;s Acts As State Machine (now known as AASM) and had many issues with it early on. Specifically I had been trying to cancel a transition in a callback and eventually found out it wasn&#8217;t supported. My understanding is that this plugin was originally built for Rails and later refactored to to be less dependent on it. state_machine however was built from day one to work with any Ruby class, and later integrations were added for ActiveRecord, Datamapper and Sequel. Its the best thing out there and I can&#8217;t recommend it enough. The code is a joy to look though and its heavily documented.
</p></description>
      <dc:subject>Programming, Ruby, Ruby on Rails</dc:subject>
      <dc:date>2009-04-01T21:14:00-05:00</dc:date>
    </item>

    <item>
      <title>CSS Grid Frameworks: 960gs Without Margins</title>
      <link>http://www.codeofficer.com/blog/entry/css_grid_frameworks_960gs_without_margins/</link>
      <guid>http://www.codeofficer.com/blog/entry/css_grid_frameworks_960gs_without_margins/#When:06:24:00Z</guid>
      <description><p>I&#8217;ve been using the <a href="http://www.blueprintcss.org/" title="Blueprint">Blueprint</a> and <a href="http://960.gs/" title="960gs">960gs</a> CSS Frameworks for a while now and have had but one complaint: they both assume you want to create your layouts with margins between the columns. Unfortunately, in my own use of these frameworks I&#8217;ve found that I work more with inner columns than exterior ones, and the default margin styles were a nuisance then.
</p>
<p>
So ... I forked <a href="http://github.com/nathansmith/960-grid-system/" title="Nathan Smith's 960gs">Nathan Smith&#8217;s 960gs</a> and recalculated the grid styling to remove the column margins. Now when I declare any two columns side by side, they will be flush against each other. Perhaps others will find this useful as well. I left the reset.css and text.css styles as they were. 
</p>
<p>
You can find my code <a href="http://github.com/CodeOfficer/960-grid-system-without-margins/" title="here">here</a>.
</p>
<p>
One final note: I&#8217;ve been using 960gs a lot more than Blueprint lately because I&#8217;ve found Blueprints text styles make too many assumptions on my behalf for how I would like my text to layout. 960gs in comparison is quite minimal.
</p>
<p>
<img src="/common/img/960cssgrid.gif" alt="960 grid without column margins" />
<br />

</p></description>
      <dc:subject>Programming, Css</dc:subject>
      <dc:date>2009-04-01T06:24:00-05:00</dc:date>
    </item>

    <item>
      <title>My March 25th Presentation on State Machines</title>
      <link>http://www.codeofficer.com/blog/entry/my_march_25th_presentation_on_state_machines/</link>
      <guid>http://www.codeofficer.com/blog/entry/my_march_25th_presentation_on_state_machines/#When:05:57:00Z</guid>
      <description><p>Materials for a talk I did on March 25th at the Maine Ruby Users Group (<a href="http://meruby.org/" title="merug.org">merug.org</a>) are now on <a href="http://github.com/CodeOfficer" title="Github">Github</a> (<a href="http://github.com/CodeOfficer/presentation-materials" title="materials">materials</a>). The topic was <b>State Machines</b> and I&#8217;ll be doing the same presentation again at the New Hampshire Ruby Users Group (<a href="http://nhruby.org/" title="nhrug.org">nhrug.org</a>) next month, on April 30th. 
</p>
<p>
This was a fun topic for me, and I can&#8217;t wait to do it again. Thanks to everyone for their kind words afterwards. <a href="http://twitter.com/renaebair" title="Renae Bair">Renae Bair</a> posted a <a href="http://www.renaebair.com/2009/03/19/ruby-conservation/" title="great review">great review</a> of the evening and of the atmosphere of our group in general. Thanks to <a href="http://dialogues.port49.com/" title="Casey Rosenthal">Casey Rosenthal</a> for his Test Driven presentation on the differences of Ruby 1.8 and 1.9 and for <a href="http://ldbss.com/wordpress/" title="Nick Hall">Nick Hall&#8217;s</a>  presentation on <a href="http://www.processing.org/" title="Ruby Proce55ing">Ruby Proce55ing</a>. 
</p>
<p>
See you all again next month!
</p></description>
      <dc:subject>Programming, Ruby</dc:subject>
      <dc:date>2009-04-01T05:57:00-05:00</dc:date>
    </item>

    <item>
      <title>Setting up Cucumber/RSpec with Rails 2.3 for Leopard</title>
      <link>http://www.codeofficer.com/blog/entry/setting_up_cucumber_rspec_with_rails_23_for_leopard/</link>
      <guid>http://www.codeofficer.com/blog/entry/setting_up_cucumber_rspec_with_rails_23_for_leopard/#When:18:58:00Z</guid>
      <description><p><b>Update:</b> See comments for resolution to a Nokogiri dependency issue.
</p>
<p>
Install the gems:
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #0000BB">sudo&nbsp;gem&nbsp;install&nbsp;rails&nbsp;</span><span style="color: #007700">--</span><span style="color: #0000BB">source&nbsp;http</span><span style="color: #007700">:</span><span style="color: #FF8000">//gems.rubyonrails.org<br /></span><span style="color: #0000BB">sudo&nbsp;gem&nbsp;install&nbsp;dchelimsky</span><span style="color: #007700">-</span><span style="color: #0000BB">rspec<br />sudo&nbsp;gem&nbsp;install&nbsp;dchelimsky</span><span style="color: #007700">-</span><span style="color: #0000BB">rspec</span><span style="color: #007700">-</span><span style="color: #0000BB">rails<br />sudo&nbsp;gem&nbsp;install&nbsp;cucumber<br />sudo&nbsp;gem&nbsp;install&nbsp;webrat</span>
</code></div><p>
</p>
<p>
Create your app:
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #0000BB">rails&nbsp;myapp<br />cd&nbsp;myapp</span>
</code></div><p>
</p>
<p>
Freeze Rails to vendor:
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #0000BB">rake&nbsp;rails</span><span style="color: #007700">:</span><span style="color: #0000BB">freeze</span><span style="color: #007700">:</span><span style="color: #0000BB">edge</span>
</code></div><p>
</p>
<p>
Manage your gem dependencies through rails, edit config/environments/test.rb. Add the following lines:
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #0000BB">config</span><span style="color: #007700">.</span><span style="color: #0000BB">gem&nbsp;</span><span style="color: #DD0000">"dchelimsky-rspec"</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">lib&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">version&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"&gt;=&nbsp;1.1.99.13"&nbsp;<br /></span><span style="color: #0000BB">config</span><span style="color: #007700">.</span><span style="color: #0000BB">gem&nbsp;</span><span style="color: #DD0000">"dchelimsky-rspec-rails"</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">lib&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">version&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"&gt;=&nbsp;1.1.99.13"<br /></span><span style="color: #0000BB">config</span><span style="color: #007700">.</span><span style="color: #0000BB">gem&nbsp;</span><span style="color: #DD0000">'aslakhellesoy-cucumber'</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">lib&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">version&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"&gt;=&nbsp;0.1.99.23<br />config.gem&nbsp;'webrat',&nbsp;:lib&nbsp;=&gt;&nbsp;false</span>
</code></div><p>
</p>
<p>
Unpack your gems:
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #0000BB">rake&nbsp;gems</span><span style="color: #007700">:</span><span style="color: #0000BB">unpack&nbsp;RAILS_ENV</span><span style="color: #007700">=</span><span style="color: #0000BB">test<br />rake&nbsp;gems</span><span style="color: #007700">:</span><span style="color: #0000BB">unpack</span><span style="color: #007700">:</span><span style="color: #0000BB">dependencies&nbsp;RAILS_ENV</span><span style="color: #007700">=</span><span style="color: #0000BB">test</span>
</code></div><p>
</p>
<p>
And generate your testing folders:
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #0000BB">script</span><span style="color: #007700">/</span><span style="color: #0000BB">generate&nbsp;rspec<br />script</span><span style="color: #007700">/</span><span style="color: #0000BB">generate&nbsp;cucumber</span>
</code></div><p>
</p>
<p>
That&#8217;s it, have fun.
</p></description>
      <dc:subject>Programming, Ruby on Rails</dc:subject>
      <dc:date>2009-03-13T18:58:00-05:00</dc:date>
    </item>

    <item>
      <title>ActiveRecords and methods on your model associations</title>
      <link>http://www.codeofficer.com/blog/entry/activerecords_and_methods_on_your_model_associations/</link>
      <guid>http://www.codeofficer.com/blog/entry/activerecords_and_methods_on_your_model_associations/#When:05:16:00Z</guid>
      <description><p>I left PHP to program in Ruby a little over a year ago now, though technically I think I focused more on learning Rails during my first month. Like most who made the journey, I found Ruby a joy to work with, and Rails to have everything I needed to make great web apps. But to this day, the part of Rails I struggle the most with is ActiveRecord. There&#8217;s just so much magic in there ... 
</p>
<p>
I want to share a syntax I learned this evening for declaring methods on an association in ActiveRecord.
</p>
<p>
In my system, Users can post content either as themselves, or as approved Affiliate identity. But since other Users might also want to post as this Affiliate, I use a has_many :through relationship to manage this. Imagine an Affiliate is a local business or organization, and Users might belong to multiple instances of them. This may explain things better.
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Affiliate&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">ActiveRecord</span><span style="color: #007700">::</span><span style="color: #0000BB">Base<br />&nbsp;&nbsp;has_many&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">affiliations<br />&nbsp;&nbsp;has_many&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">users</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">through&nbsp;</span><span style="color: #007700">=&gt;&nbsp;:</span><span style="color: #0000BB">affiliations<br />&nbsp;&nbsp;attr_accessor&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">status<br />end<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Affiliation&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">ActiveRecord</span><span style="color: #007700">::</span><span style="color: #0000BB">Base<br />&nbsp;&nbsp;belongs_to&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">affiliate<br />&nbsp;&nbsp;belongs_to&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">user<br />&nbsp;&nbsp;attr_accessor&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">status<br />end<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">User&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">ActiveRecord</span><span style="color: #007700">::</span><span style="color: #0000BB">Base<br />&nbsp;&nbsp;has_many&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">affiliations<br />&nbsp;&nbsp;has_many&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">affiliates</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">through&nbsp;</span><span style="color: #007700">=&gt;&nbsp;:</span><span style="color: #0000BB">affiliations<br />end</span>
</code></div><p>
</p>
<p>
I needed to create a select menu for Users creating content in my system so they could select an optional alternate Affiliate identity to post as. The thing is ... these relationships aren&#8217;t cut and dry. Both the Affiliate and the Affiliation need to have a status of &#8216;approved&#8217; or that User cant post as that Affiliate identity.
</p>
<p>
That said, I needed to populate this select menu for the User. At first I started thinking like this:
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">User&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">ActiveRecord</span><span style="color: #007700">::</span><span style="color: #0000BB">Base<br />&nbsp;&nbsp;has_many&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">affiliations<br />&nbsp;&nbsp;has_many&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">affiliates</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">through&nbsp;</span><span style="color: #007700">=&gt;&nbsp;:</span><span style="color: #0000BB">affiliations<br /><br />&nbsp;&nbsp;def&nbsp;approved_affiliates<br />&nbsp;&nbsp;&nbsp;&nbsp;affiliates</span><span style="color: #007700">.</span><span style="color: #0000BB">with_status</span><span style="color: #007700">(:</span><span style="color: #0000BB">approved</span><span style="color: #007700">).</span><span style="color: #0000BB">find</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</span><span style="color: #0000BB">all</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:include&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">&#91;</span><span style="color: #007700">:</span><span style="color: #0000BB">affiliations&#93;</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</span><span style="color: #0000BB">conditions&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">&#91;</span><span style="color: #DD0000">"affiliations.status&nbsp;=&nbsp;'approved'"</span><span style="color: #0000BB">&#93;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;</span><span style="color: #0000BB">end&nbsp;<br />end<br /><br /></span><span style="color: #007700">@</span><span style="color: #0000BB">affiliates&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">current_user</span><span style="color: #007700">.</span><span style="color: #0000BB">approved_affiliates</span><span style="color: #007700">.</span><span style="color: #0000BB">collect&nbsp;&#123;</span><span style="color: #007700">|</span><span style="color: #0000BB">p</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">&#91;&nbsp;p</span><span style="color: #007700">.</span><span style="color: #0000BB">name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">p</span><span style="color: #007700">.</span><span style="color: #0000BB">id</span><span style="color: #007700">.</span><span style="color: #0000BB">to_s&nbsp;&#93;&nbsp;&#125;</span>
</code></div><p>
</p>
<p>
But looking in my User model, this didn&#8217;t seem very elegant and I wanted to find a way to keep the method definition attached to the code where I declare the association. I was already using a named_scope in my Affiliates model &#8220;with_status(:approved)&#8221; (not shown here) and so I thought I might be able to combine it with a similar named scope on the join.
</p>
<p>
So, really what I needed was an association method to scope the Affiliate AND the Affiliation to have statuses of &#8216;approved&#8217;. Try as I might I couldn&#8217;t figure out the syntax for this. If you know how it can be done, please do share.
</p>
<p>
What I ended up with, and am quite happy with is ...
</p>
<p>
</p><div class="codeblock"><code>
<span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">User&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">ActiveRecord</span><span style="color: #007700">::</span><span style="color: #0000BB">Base<br />&nbsp;&nbsp;has_many&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">affiliations<br />&nbsp;&nbsp;has_many&nbsp;</span><span style="color: #007700">:</span><span style="color: #0000BB">affiliates</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">through&nbsp;</span><span style="color: #007700">=&gt;&nbsp;:</span><span style="color: #0000BB">affiliations&nbsp;</span><span style="color: #007700">do<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">def&nbsp;with_combined_statuses</span><span style="color: #007700">(</span><span style="color: #0000BB">status</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">find</span><span style="color: #007700">(:</span><span style="color: #0000BB">all</span><span style="color: #007700">,&nbsp;:include&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">&#91;</span><span style="color: #007700">:</span><span style="color: #0000BB">affiliations&#93;</span><span style="color: #007700">,&nbsp;:</span><span style="color: #0000BB">conditions&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">&#91;</span><span style="color: #DD0000">"affiliations.status&nbsp;=&nbsp;?&nbsp;AND&nbsp;affiliates.status&nbsp;=&nbsp;?"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">status</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">status&#93;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">end<br />&nbsp;&nbsp;end<br />end<br /><br /></span><span style="color: #007700">@</span><span style="color: #0000BB">affiliates&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">current_user</span><span style="color: #007700">.</span><span style="color: #0000BB">affiliates</span><span style="color: #007700">.</span><span style="color: #0000BB">with_combined_statuses</span><span style="color: #007700">(</span><span style="color: #DD0000">'approved'</span><span style="color: #007700">).</span><span style="color: #0000BB">collect&nbsp;&#123;</span><span style="color: #007700">|</span><span style="color: #0000BB">p</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">&#91;&nbsp;p</span><span style="color: #007700">.</span><span style="color: #0000BB">name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">p</span><span style="color: #007700">.</span><span style="color: #0000BB">id</span><span style="color: #007700">.</span><span style="color: #0000BB">to_s&nbsp;&#93;&nbsp;&#125;</span>
</code></div><p>
</p>
<p>
So, I learned you can pass a block to the association and declare methods there, as well, those methods can accept parameters.
</p>
<p>
Yay me <img src="http://www.codeofficer.com/images/smileys/smile.gif" width="19" height="19" alt="smile" style="border:0;" />
</p></description>
      <dc:subject>Programming, Ruby on Rails</dc:subject>
      <dc:date>2009-03-08T05:16:00-05:00</dc:date>
    </item>

    
    </channel>
</rss>
