# Find instead of find\_by\_sql

**URL:** <https://www.ruby-forum.com/t/find-instead-of-find-by-sql/195145>\
**Category:** Rails\
**Created:** [September 17, 2010, 5:02pm UTC](https://www.ruby-forum.com/t/find-instead-of-find-by-sql/195145 "2010-09-17T17:02:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 17, 2010, 5:02pm UTC](https://www.ruby-forum.com/t/find-instead-of-find-by-sql/195145/1 "2010-09-17T17:02:03Z")

</div>

Dear all,

I need to apply “find” instead of find\_by\_sql. Kindly suggest me.  
Because,  
params[gm] is not working with find\_by\_sql. Kindly justify whether my  
code  
below is in correct syntax…

find\_by\_sql(“SELECT column1 FROM table WHERE column2 = ‘params[gm]’  
ORDER BY  
column1”)

–  
With Regards  
Palani Kannan. K  
Office: +49-531-2616-226  
Mobile: +4917647098166

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 17, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/find-instead-of-find-by-sql/195145/2 "2010-09-17T17:27:14Z")

</div>

On Sep 17, 2010, at 11:01 AM, PalaniKannan K wrote:

> With Regards  
> Palani Kannan. K  
> Office: +49-531-2616-226  
> Mobile: +4917647098166

Well, depending on your version of ActiveRecord (and assuming that  
your model is called Model)

Rails 2:  
Model.find(:all, :select =\> ‘column1’, :conditions =\> { :column2 =\>  
params[gm] }, :order =\> ‘column1’)

Rails 3:  
Model.select(‘column1’).where(:column2 =\>  
params[gm]).order(‘column1’).all

But that will give you anemic and readonly instances of Model because  
they don’t have their id column.

If you want fully formed Model instances, don’t specify the select part.

If you just want the values of column1, you could do something like:

Model.connection.select\_values(Model.send(:sanitize\_sql, [“SELECT  
DISTINCT column1 FROM #{Model.table\_name} WHERE column2 = ? ORDER BY  
column1”, params[gm]))

But that’s obviously a bit ugly.

Perhaps you should ask with REAL names from your application so that  
the answers can be made more naturally.

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 17, 2010, 5:30pm UTC](https://www.ruby-forum.com/t/find-instead-of-find-by-sql/195145/3 "2010-09-17T17:30:45Z")

</div>

Try this:

find\_by\_sql(“SELECT column1 FROM table WHERE column2 = ? ORDER BY  
column1”,params[:gm])

More here:

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-find_by_sql)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

Walter

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 17, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/find-instead-of-find-by-sql/195145/4 "2010-09-17T17:50:39Z")

</div>

On Sep 17, 2010, at 11:42 AM, PalaniKannan K wrote:

> Dear Rob,
> 
> I got it… Thank you for help…
> 
> But, I applied code of Rails 2 in Rails 3, It worked for me. It may  
> lead any problem with Rails 2 code in future?
> 
> –  
> With Regards  
> Palani Kannan. K

It should work fine until Rails 3.1 (when the older ActiveRecord forms  
will be deprecated)

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 17, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/find-instead-of-find-by-sql/195145/5 "2010-09-17T17:43:38Z")

</div>

Dear Rob,

I got it… Thank you for help…

But, I applied code of Rails 2 in Rails 3, It worked for me. It may lead  
any  
problem with Rails 2 code in future?

–  
With Regards  
Palani Kannan. K

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 17, 2010, 5:52pm UTC](https://www.ruby-forum.com/t/find-instead-of-find-by-sql/195145/6 "2010-09-17T17:52:24Z")

</div>

Dear Rob,

Thank you for information. I will change all my Rails2 code to Rails3.  
It  
will be better in future.

On 17 September 2010 17:49, Rob B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> With Regards  
> removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
With Regards  
Palani Kannan. K  
Office: +49-531-2616-226  
Mobile: +4917647098166
