<?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>Jeroen van Doorn &#187; Active Record</title>
	<atom:link href="http://www.jeroenvandoorn.nl/tag/active-record/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeroenvandoorn.nl</link>
	<description>On mission!</description>
	<lastBuildDate>Tue, 21 Jul 2009 08:28:56 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ActiveRecord find and MySql &#8216;IN&#8217; gotcha</title>
		<link>http://www.jeroenvandoorn.nl/2009/07/activerecord-find-and-mysql-in-gotcha/</link>
		<comments>http://www.jeroenvandoorn.nl/2009/07/activerecord-find-and-mysql-in-gotcha/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 08:27:04 +0000</pubDate>
		<dc:creator>Jeroen van Doorn</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Active Record]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.jeroenvandoorn.nl/?p=3</guid>
		<description><![CDATA[In my latest project I needed to fetch a group of tasks who where linked to a  group of categories. All that I have are the Id&#8217;s of te categories which made me write the following code

Tasks.find&#40;:all, 
    :conditions =&#62; &#91;&#34;categorie_id IN (?)&#34;, @categories.join&#40;','&#41;&#93;&#41;

It seemed to work, but apparently I didn&#8217;t test [...]]]></description>
			<content:encoded><![CDATA[<p>In my latest project I needed to fetch a group of tasks who where linked to a  group of categories. All that I have are the Id&#8217;s of te categories which made me write the following code</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Tasks.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, 
    <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;categorie_id IN (?)&quot;</span>, <span style="color:#0066ff; font-weight:bold;">@categories</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>It seemed to work, but apparently I didn&#8217;t test it right and my tests where not sufficient enough.</p>
<p>This code produces the following SQL:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`tasks`</span> <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #FF00FF;">&#40;</span>categorie_id <span style="color: #990099; font-weight: bold;">IN</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'1,2,3,4'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span></pre></div></div>

<p>This is obviously wrong. You&#8217;ll get quotes around the row of id&#8217;s which is kind of logical if you think about it. Afteral it&#8217;s a string. To prevent this from happening, simply apply the following syntax</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Tasks.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>,
    <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;categorie_id IN (#{@categories.join(',')})&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jeroenvandoorn.nl/2009/07/activerecord-find-and-mysql-in-gotcha/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
