<?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>Wirestorm &#187; Criteria</title>
	<atom:link href="http://wirestorm.net/blog/category/projects/criteria/feed/?category_name=projects/criteria" rel="self" type="application/rss+xml" />
	<link>http://wirestorm.net/blog</link>
	<description>Ray Hilton&#039;s blog about interesting technology</description>
	<lastBuildDate>Mon, 12 Dec 2011 11:07:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2-bleeding</generator>
		<item>
		<title>Criteria for ActiveRecord</title>
		<link>http://wirestorm.net/blog/2008/04/06/criteria-for-activerecord/</link>
		<comments>http://wirestorm.net/blog/2008/04/06/criteria-for-activerecord/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 08:00:39 +0000</pubDate>
		<dc:creator>rayh</dc:creator>
				<category><![CDATA[Criteria]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://wirestorm.net/blog/?p=53</guid>
		<description><![CDATA[Over the last couple of days I&#8217;ve been working on a Criteria for ActiveRecord. The concept is quite simple; rather than writing strings of SQL to pass to the find() method, we build the criteria using an object-orientated approach. Anyone who has used Hibernate, Torque, Propel et al will know how powerful and useful this [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last couple of days I&#8217;ve been working on a Criteria for ActiveRecord.  The concept is quite simple; rather than writing strings of SQL to pass to the find() method, we build the criteria using an object-orientated approach.  Anyone who has used Hibernate, Torque, Propel et al will know how powerful and useful this can be.  </p>
<p>For example, imagine you have a series of filters, all of which may, or may not, add constraints or expressions to your query.  Each filter will need to append it&#8217;s own SQL to the end of any previously generated SQL and may even need to introspect the existing SQL to determine if its doubling up on statements or resolve potential conflicts.</p>
<p>With Criteria this becomes simpler.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">criteria = Criteria.<span style="color:#9900CC;">new</span>
criteria.<span style="color:#9966CC; font-weight:bold;">and</span> <span style="color:#9966CC; font-weight:bold;">do</span> |c|
  c.<span style="color:#9966CC; font-weight:bold;">or</span> User.<span style="color:#9900CC;">role</span>.<span style="color:#9900CC;">eq</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:admin</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  c.<span style="color:#9966CC; font-weight:bold;">or</span> User.<span style="color:#9900CC;">active</span>.<span style="color:#9900CC;">eq</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  c.<span style="color:#9966CC; font-weight:bold;">or</span> User.<span style="color:#9900CC;">createdAt</span>.<span style="color:#9900CC;">gt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">10</span>.<span style="color:#9900CC;">hours</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
criteria.<span style="color:#9966CC; font-weight:bold;">and</span> <span style="color:#9966CC; font-weight:bold;">do</span> |c|
  c.<span style="color:#9966CC; font-weight:bold;">or</span> User.<span style="color:#9900CC;">role</span>.<span style="color:#9900CC;">eq</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:editor</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  c.<span style="color:#9966CC; font-weight:bold;">or</span> User.<span style="color:#9900CC;">active</span>.<span style="color:#9900CC;">eq</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  c.<span style="color:#9966CC; font-weight:bold;">or</span> <span style="color:#9966CC; font-weight:bold;">do</span> |c2|
    c2.<span style="color:#9966CC; font-weight:bold;">and</span> User.<span style="color:#9900CC;">createdAt</span>.<span style="color:#9900CC;">gt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">20</span>.<span style="color:#9900CC;">days</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    c2.<span style="color:#9966CC; font-weight:bold;">and</span> User.<span style="color:#9900CC;">createdAt</span>.<span style="color:#9900CC;">lt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">10</span>.<span style="color:#9900CC;">hours</span>.<span style="color:#9900CC;">ago</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>
users = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, criteria<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>We could, at any point, introspect the state of criteria by calling:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">criteria.<span style="color:#9900CC;">ands</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |c|
  put <span style="color:#996600;">&quot;AND #{c}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
criteria.<span style="color:#9900CC;">ors</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |c|
  put <span style="color:#996600;">&quot;OR #{c}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And, finally, we can remove arbitrary bits of criteria:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">criteria.<span style="color:#9900CC;">ands</span>.<span style="color:#9900CC;">delete_at</span> <span style="color:#006666;">2</span></pre></div></div>

<p>I hope to have a release out very soon, but for the time being some more examples and API docs are  on the <a href="http://criteria.rubyforge.org/">Criteria project page</a> on <a href="http://rubyforge.org">RubyForge</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wirestorm.net/blog/2008/04/06/criteria-for-activerecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

