{"id":89,"date":"2006-05-11T00:20:31","date_gmt":"2006-05-11T05:20:31","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/05\/11\/rails-like-sql\/"},"modified":"2006-05-11T00:20:31","modified_gmt":"2006-05-11T05:20:31","slug":"rails-like-sql","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/05\/11\/rails-like-sql\/","title":{"rendered":"Rails Like SQL"},"content":{"rendered":"<p>If you have played with Ruby on Rails you may already be familiar with the ActiveRecord&#8217;s find method.  Here is a recap, say I have a Item model:<\/p>\n<pre>\nclass Item &lt; ActiveRecord::Base\nend\n<\/pre>\n<p>In your controller you can use the Item class to find all items that meet a given criteria.  Here are some examples:<\/p>\n<pre>\n# Find the lowest priced item\nlowest = Item.find(:first, :order =&gt; 'price asc')\n# Find all items by category\nitems = Item.find(:all, :conditions =&gt; [\"category = ?\", params[:category])\n<\/pre>\n<p>What I couldn&#8217;t fine anywhere is, how to use the ActiveRecord&#8217;s find method to find items whose name is like a given parameter.  In SQL I could do something like this:<\/p>\n<pre>\nselect * from items where name like 'ItemName%';\n<\/pre>\n<p>My first attempt at solving this left me open to hacks via SQL injection.  After some thought it occurred to me that I could do the following:<\/p>\n<pre>\nlike = params[:name].concat(\"%\")\nitems = Item.find(:all, :conditions =&gt; [\"name like ?\", like])\n<\/pre>\n<p>I still haven&#8217;t found the official way to accomplish the above but in the mean time this gets the job done.<\/p>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/ruby\" rel=\"tag\">ruby<\/a>, <a href=\"http:\/\/technorati.com\/tag\/sql\" rel=\"tag\"> sql<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rails\" rel=\"tag\"> rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/ruby+on+rails\" rel=\"tag\"> ruby on rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/sql+injection\" rel=\"tag\"> sql injection<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have played with Ruby on Rails you may already be familiar with the ActiveRecord&#8217;s find method. Here is a recap, say I have a Item model: class Item &lt; ActiveRecord::Base end In your controller you can use the Item class to find all items that meet a given criteria. Here are some examples: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[22,23,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-1r","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/89"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}