<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1887</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1887" rel="self"/>
  <title>David</title>
  <updated>Mon Jan 18 21:24:53 -0800 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor417803</id>
    <published>2010-01-18T21:24:53-08:00</published>
    <title>[Ruby] On ActiveRecord find_by_sql</title>
    <content type="html">&lt;p&gt;I thought that the [&amp;quot;?&amp;quot;, var] pattern protected from sql injection like it says in the docs, no?&lt;/p&gt;

&lt;pre&gt;## From railsguides [ruby]
# But in SQL fragments, especially in conditions fragments (:conditions =&amp;gt; &amp;quot;...&amp;quot;), the connection.execute()
# or Model.find_by_sql() methods, it has to be applied manually. Instead of passing a string to the conditions
# option, you can pass an array to sanitize tainted strings like this:

Model.find(:first, :conditions =&amp;gt; [&amp;quot;login = ? AND password = ?&amp;quot;, entered_user_name, entered_password])

# As you can see, the first part of the array is an SQL fragment with question marks. The sanitized versions
# of the variables in the second part of the array replace the question marks. Or you can pass a hash for the same result:&lt;/pre&gt;</content>
    <author>
      <name>David</name>
      <email></email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1149-activerecord-find_by_sql/refactors/417803" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor417666</id>
    <published>2010-01-18T16:10:42-08:00</published>
    <title>[Ruby] On ActiveRecord find_by_sql</title>
    <content type="html">&lt;p&gt;Thanks. Updated now.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>David</name>
      <email></email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1149-activerecord-find_by_sql/refactors/417666" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1149</id>
    <published>2010-01-17T20:52:15-08:00</published>
    <updated>2010-01-21T03:33:18-08:00</updated>
    <title>[Ruby] ActiveRecord find_by_sql</title>
    <content type="html">&lt;p&gt;How can I turn this into something more activerecordish? I might want to add another condition into that query, like limiting the search to clips after a specific date.&lt;/p&gt;

&lt;pre&gt;Clip.find_by_sql([&amp;quot;select * from clips where not exists
        (select 'clip_id' from histories where histories.clip_id = clips.id
        and histories.user_id = ?) ORDER BY clips.created_at DESC LIMIT 20&amp;quot;, user.id])&lt;/pre&gt;</content>
    <author>
      <name>David</name>
      <email></email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1149-activerecord-find_by_sql" rel="alternate"/>
  </entry>
</feed>

