<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: ActiveRecord Query Building with Multiple and Optional Conditions</title>
	<atom:link href="http://unixmonkey.net/?feed=rss2&#038;p=31" rel="self" type="application/rss+xml" />
	<link>http://unixmonkey.net/?p=31</link>
	<description>creative engineering and technological abuse</description>
	<lastBuildDate>Thu, 05 Aug 2010 19:47:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: brett</title>
		<link>http://unixmonkey.net/?p=31&#038;cpage=1#comment-22896</link>
		<dc:creator>brett</dc:creator>
		<pubDate>Sat, 16 Jan 2010 03:34:11 +0000</pubDate>
		<guid isPermaLink="false">http://unixmonkey.net/?p=31#comment-22896</guid>
		<description>Another NOOB here.  Took me forever to find this article.  I assume this is still up to date given that it&#039;s only a year old.  I was starting to get worried that I was missing something really obvious.  Thanks so much!</description>
		<content:encoded><![CDATA[<p>Another NOOB here.  Took me forever to find this article.  I assume this is still up to date given that it&#8217;s only a year old.  I was starting to get worried that I was missing something really obvious.  Thanks so much!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Hoffman</title>
		<link>http://unixmonkey.net/?p=31&#038;cpage=1#comment-22326</link>
		<dc:creator>Mark Hoffman</dc:creator>
		<pubDate>Thu, 31 Dec 2009 22:22:09 +0000</pubDate>
		<guid isPermaLink="false">http://unixmonkey.net/?p=31#comment-22326</guid>
		<description>Another Rails newbie here and I was trying to figure out how to string all my parameters into a query and Google brought me here. I&#039;m sure the other plugins mentioned will work fine, but the approach you gave is clean and does what I need. Thanks for posting it!</description>
		<content:encoded><![CDATA[<p>Another Rails newbie here and I was trying to figure out how to string all my parameters into a query and Google brought me here. I&#8217;m sure the other plugins mentioned will work fine, but the approach you gave is clean and does what I need. Thanks for posting it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniele</title>
		<link>http://unixmonkey.net/?p=31&#038;cpage=1#comment-20705</link>
		<dc:creator>Daniele</dc:creator>
		<pubDate>Thu, 19 Nov 2009 11:26:35 +0000</pubDate>
		<guid isPermaLink="false">http://unixmonkey.net/?p=31#comment-20705</guid>
		<description>Great article! I&#039;m a newbie, coming from the Java world and I was looking for something similar to Hibernte Criteria with ActiveRecord. I think I found it here. I&#039;m still learning the basics of Ruby and Rails, so I currently prefer your approach than the plugin one. Great snippet.</description>
		<content:encoded><![CDATA[<p>Great article! I&#8217;m a newbie, coming from the Java world and I was looking for something similar to Hibernte Criteria with ActiveRecord. I think I found it here. I&#8217;m still learning the basics of Ruby and Rails, so I currently prefer your approach than the plugin one. Great snippet.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unixmonkey</title>
		<link>http://unixmonkey.net/?p=31&#038;cpage=1#comment-11154</link>
		<dc:creator>unixmonkey</dc:creator>
		<pubDate>Tue, 30 Dec 2008 20:36:13 +0000</pubDate>
		<guid isPermaLink="false">http://unixmonkey.net/?p=31#comment-11154</guid>
		<description>@jqr, I pulled your gist into the comment.

If I see code in my comments, I go back after the fact and edit the right tags in.</description>
		<content:encoded><![CDATA[<p>@jqr, I pulled your gist into the comment.</p>
<p>If I see code in my comments, I go back after the fact and edit the right tags in.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elijah Miller</title>
		<link>http://unixmonkey.net/?p=31&#038;cpage=1#comment-11145</link>
		<dc:creator>Elijah Miller</dc:creator>
		<pubDate>Tue, 30 Dec 2008 03:32:52 +0000</pubDate>
		<guid isPermaLink="false">http://unixmonkey.net/?p=31#comment-11145</guid>
		<description>I&#039;ve written more than my share of searches like you describe, but now I tend to use named_scopes for clarity and ease of composing complex conditions.

Check it: http://gist.github.com/41500
&lt;pre lang=&quot;ruby&quot;&gt;
class Something &lt; ActiveRecord::Base
  # Simpler approach available with this small Rails patch:
  # http://github.com/jqr/rails/commit/d46d7b3cc97d9b4dc4dcd3cc210e645a5497a9b1
  #
  # named_scope :with_town_like, lambda { &#124;term&#124;
  # { :conditions =&gt; [&#039;town LIKE ?&#039;, term] } unless term.blank?
  # }
  #
  # named_scope :with_hobby, lambda { &#124;term&#124;
  # { :conditions =&gt; [&#039;hobby = ?&#039;, term] } unless term.blank?
  # }
  #
  # named_scope :age_at_least, lambda { &#124;term&#124;
  # { :conditions =&gt; [&#039;age &gt;= ?&#039;, term] } unless term.blank?
  # }
 
  named_scope :with_town_like, lambda { &#124;term&#124;
    if !term.blank?
      { :conditions =&gt; [&#039;town LIKE ?&#039;, term] }
    else
      {}
    end
  }
 
  named_scope :with_hobby, lambda { &#124;term&#124;
    if !term.blank?
      { :conditions =&gt; [&#039;hobby = ?&#039;, term] }
    else
      {}
    end
  }
 
  named_scope :age_at_least, lambda { &#124;term&#124;
    if !term.blank?
      { :conditions =&gt; [&#039;age &gt;= ?&#039;, term] }
    else
      {}
    end
  }
 
  def search(options = {})
    town_like(options[:town]).
    with_hobby(options[:hobby]).
    age_at_least(options[:age])
  end
end
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I&#8217;ve written more than my share of searches like you describe, but now I tend to use named_scopes for clarity and ease of composing complex conditions.</p>
<p>Check it: <a href="http://gist.github.com/41500" rel="nofollow">http://gist.github.com/41500</a></p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Something &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#008000; font-style:italic;"># Simpler approach available with this small Rails patch:</span>
  <span style="color:#008000; font-style:italic;"># http://github.com/jqr/rails/commit/d46d7b3cc97d9b4dc4dcd3cc210e645a5497a9b1</span>
  <span style="color:#008000; font-style:italic;">#</span>
  <span style="color:#008000; font-style:italic;"># named_scope :with_town_like, lambda { |term|</span>
  <span style="color:#008000; font-style:italic;"># { :conditions =&gt; ['town LIKE ?', term] } unless term.blank?</span>
  <span style="color:#008000; font-style:italic;"># }</span>
  <span style="color:#008000; font-style:italic;">#</span>
  <span style="color:#008000; font-style:italic;"># named_scope :with_hobby, lambda { |term|</span>
  <span style="color:#008000; font-style:italic;"># { :conditions =&gt; ['hobby = ?', term] } unless term.blank?</span>
  <span style="color:#008000; font-style:italic;"># }</span>
  <span style="color:#008000; font-style:italic;">#</span>
  <span style="color:#008000; font-style:italic;"># named_scope :age_at_least, lambda { |term|</span>
  <span style="color:#008000; font-style:italic;"># { :conditions =&gt; ['age &gt;= ?', term] } unless term.blank?</span>
  <span style="color:#008000; font-style:italic;"># }</span>
&nbsp;
  named_scope <span style="color:#ff3333; font-weight:bold;">:with_town_like</span>, <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |term|
    <span style="color:#9966CC; font-weight:bold;">if</span> !term.<span style="color:#9900CC;">blank</span>?
      <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:conditions</span> =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'town LIKE ?'</span>, term<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  named_scope <span style="color:#ff3333; font-weight:bold;">:with_hobby</span>, <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |term|
    <span style="color:#9966CC; font-weight:bold;">if</span> !term.<span style="color:#9900CC;">blank</span>?
      <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:conditions</span> =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'hobby = ?'</span>, term<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  named_scope <span style="color:#ff3333; font-weight:bold;">:age_at_least</span>, <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |term|
    <span style="color:#9966CC; font-weight:bold;">if</span> !term.<span style="color:#9900CC;">blank</span>?
      <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:conditions</span> =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'age &gt;= ?'</span>, term<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> search<span style="color:#006600; font-weight:bold;">&#40;</span>options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    town_like<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:town</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.
    <span style="color:#9900CC;">with_hobby</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:hobby</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.
    <span style="color:#9900CC;">age_at_least</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:age</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Nolan Eakins</title>
		<link>http://unixmonkey.net/?p=31&#038;cpage=1#comment-11144</link>
		<dc:creator>Nolan Eakins</dc:creator>
		<pubDate>Tue, 30 Dec 2008 03:25:28 +0000</pubDate>
		<guid isPermaLink="false">http://unixmonkey.net/?p=31#comment-11144</guid>
		<description>After doing stuff like the above more than once a while back, I dug up Condition Builder:
&lt;a href=&quot;http://blog.inquirylabs.com/2007/01/04/condition-builder-10-released/&quot; rel=&quot;nofollow&quot;&gt;http://blog.inquirylabs.com/2007/01/04/condition-builder-10-released/&lt;/a&gt;
It keeps you from having to keep track of both the condition string and values.</description>
		<content:encoded><![CDATA[<p>After doing stuff like the above more than once a while back, I dug up Condition Builder:<br />
<a href="http://blog.inquirylabs.com/2007/01/04/condition-builder-10-released/" rel="nofollow">http://blog.inquirylabs.com/2007/01/04/condition-builder-10-released/</a><br />
It keeps you from having to keep track of both the condition string and values.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
