<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:refactormycode.com,2007:users198</id>
  <link type="application/atom+xml" href="http://refactormycode.com/users/198" rel="self"/>
  <title>erockenjew.myopenid.com</title>
  <updated>Tue Oct 16 04:02:23 +0000 2007</updated>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor437</id>
    <published>2007-10-16T04:02:23+00:00</published>
    <title>[Ruby] On Merging two sets of conditions</title>
    <content type="html">&lt;p&gt;BTW, i know switching to ruby-sequel isn't actually an acceptable solution, just wanted to point out how the :conditions =&amp;gt; in ActiveRecord::Base.find is not the best implementation of building SQL statements.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>erockenjew.myopenid.com</name>
      <email>ericallam@gmail.com</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/84-merging-two-sets-of-conditions/refactors/437" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor436</id>
    <published>2007-10-16T04:00:42+00:00</published>
    <title>[Ruby] On Merging two sets of conditions</title>
    <content type="html">&lt;p&gt;Or you could just switch to using ruby-sequel instead of ActiveRecord.  ruby-sequel defines a flued interface for building queries, and queries are 'lazy-loaded' which means they don't get executed until they need to be (such as when you call .each)&lt;/p&gt;

&lt;pre&gt;DB[:users].filter(:country =&amp;gt; &amp;quot;US&amp;quot;).or(&amp;quot;confirmed = ?&amp;quot;, 1).filter(&amp;quot;ids NOT IN (?)&amp;quot;, [1, 10, 100])
#=&amp;gt; SELECT * FROM users WHERE country = 'US' OR confirmed = 1 AND ids NOT IN (1,10,100)&lt;/pre&gt;</content>
    <author>
      <name>erockenjew.myopenid.com</name>
      <email>ericallam@gmail.com</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/84-merging-two-sets-of-conditions/refactors/436" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor434</id>
    <published>2007-10-16T02:44:17+00:00</published>
    <title>[Ruby] On Merging two sets of conditions</title>
    <content type="html">&lt;p&gt;I wrote an extremely small class to deal with this same problem, which makes use of ActiveRecord::Base.sanitize_sql, and makes it a little more apparent in the code what is happening.  Its also really easy to test.&lt;/p&gt;

&lt;pre&gt;# used for building a conditions string for an ActiveRecord find
# 
# usage:
#   
#   conditions = Conditioner.new
#   conditions &amp;lt;&amp;lt; ['open = ? AND user_id = ?', 1, @user.id]
#   conditions &amp;lt;&amp;lt; ['search_term LIKE ? ', &amp;quot;%#{params[:search_term]}&amp;quot;]
#   
#   Opportunity.find(:all, :conditions =&amp;gt; conditions.to_conditions)
#   # select * from opportunity WHERE open = 1 AND user_id = 1 AND search_term LIKE '%hello%'
class Conditioner
  
  attr_reader :conditions 

  def initialize
    @conditions = []
  end
  
  # adds an array to the conditioner
  #
  # usage:
  #
  #   conditions = Conditioner.new
  #   conditions &amp;lt;&amp;lt; ['open = ? AND user_id = ?', 1, @user.id]
  #
  def &amp;lt;&amp;lt;(con)
    @conditions &amp;lt;&amp;lt; ActiveRecord::Base.send(:sanitize_sql_array, con)
  end
  
  alias_method :add, :&amp;lt;&amp;lt;
  
  
  # outputs the conditions in a string
  #
  # params:
  #
  #  * seperator (choose either AND or OR, defaults to AND)
  def to_conditions(seperator=&amp;quot;AND&amp;quot;)
    @conditions.join(&amp;quot; #{seperator} &amp;quot;)
  end
  
  
end&lt;/pre&gt;</content>
    <author>
      <name>erockenjew.myopenid.com</name>
      <email>ericallam@gmail.com</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/84-merging-two-sets-of-conditions/refactors/434" rel="alternate"/>
  </entry>
</feed>
