# Find\_by\_sql

**URL:** <https://www.ruby-forum.com/t/find-by-sql/145143>\
**Category:** Rails\
**Created:** [August 6, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/find-by-sql/145143 "2008-08-06T13:12:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gasthana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@gasthana](https://www.ruby-forum.com/u/gasthana)\
**Post date:** [August 6, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/find-by-sql/145143/1 "2008-08-06T13:12:56Z")

</div>

hi,

i have to fire below query…  
SELECT \* FROM clarify\_crs c where c.id IN (select cscr.cscr\_cr\_id from  
clarify\_case\_crs cscr where cscr.cscr\_case\_id=10001);

I have to pass this 10001 as some variable. So i am using below query,

cs\_id=params[:id]  
@lnkcr = Cl.find\_by\_sql(‘SELECT \* FROM crs c WHERE c.id IN (SELECT  
cscr.cscr\_cr\_id FROM case\_crs cscr WHERE cscr.cscr\_case\_id=?)’,cs\_id)

but i am getting error message saying that “wrong number of arguments (2  
for 1)”.

Please help. it is very important to me…

---

<div class="post-metadata">

**Author:** ![gasthana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@gasthana](https://www.ruby-forum.com/u/gasthana)\
**Post date:** [August 6, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/find-by-sql/145143/2 "2008-08-06T14:21:28Z")

</div>

find\_by\_sql doesn’t work as the :conditions in normal finds.  
just use:

@lnkcr = Cl.find\_by\_sql(“SELECT \* FROM crs c WHERE c.id IN (SELECT  
cscr.cscr\_cr\_id FROM case\_crs cscr WHERE cscr.cscr\_case\_id=#{cs\_id})”)

---

<div class="post-metadata">

**Author:** ![gasthana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@gasthana](https://www.ruby-forum.com/u/gasthana)\
**Post date:** [August 6, 2008, 2:50pm UTC](https://www.ruby-forum.com/t/find-by-sql/145143/3 "2008-08-06T14:50:14Z")

</div>

It works…  
Thank you very much Thorsten…

Thorsten M. wrote:

> find\_by\_sql doesn’t work as the :conditions in normal finds.  
> just use:
> 
> @lnkcr = Cl.find\_by\_sql(“SELECT \* FROM crs c WHERE c.id IN (SELECT  
> cscr.cscr\_cr\_id FROM case\_crs cscr WHERE cscr.cscr\_case\_id=#{cs\_id})”)
