# Find\_by\_sql

**URL:** <https://www.ruby-forum.com/t/find-by-sql/166514>\
**Category:** Rails\
**Created:** [April 26, 2009, 5:48pm UTC](https://www.ruby-forum.com/t/find-by-sql/166514 "2009-04-26T17:48:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dmarc](https://www.ruby-forum.com/u/dmarc)\
**Post date:** [April 26, 2009, 5:48pm UTC](https://www.ruby-forum.com/t/find-by-sql/166514/1 "2009-04-26T17:48:38Z")

</div>

I’m having some difficulty getting find\_by\_sql to work.

This code below works perfect:

@available = Elevator.find\_by\_sql(“select name from elevator\_timeslots  
WHERE NOT EXISTS(select elevator\_timeslot\_id from elevators where  
elevators.elevator\_timeslot\_id = elevator\_timeslots.id)”)

Now I’m trying to add one more condition which is causing me grief:

@available = Elevator.find\_by\_sql(“select name from elevator\_timeslots  
WHERE NOT EXISTS(select elevator\_timeslot\_id from elevators where  
elevators.elevator\_timeslot\_id = elevator\_timeslots.id  
and elevators.booked\_date = ?”, params[:booked\_date])) \*\* Added line  
not working

Basically using

and elevators.booked\_date = ?, params[:booked\_date] is causing only  
errors.

Any advise, I truely appreciate it.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dmarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dmarc](https://www.ruby-forum.com/u/dmarc)\
**Post date:** [April 26, 2009, 10:20pm UTC](https://www.ruby-forum.com/t/find-by-sql/166514/2 "2009-04-26T22:20:49Z")

</div>

On Apr 26, 4:48 pm, Darren S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Basically using
> 
> and elevators.booked\_date = ?, params[:booked\_date] is causing only  
> errors.
> 
> Any advise, I truely appreciate it.

much like the :conditions option requires either a string, or an  
array containing a string and the parameter values (or a hash, but  
that’s irrelevant here), you need to either pass a single string to  
find\_by\_sql or an array, ie Foo.find\_by\_sql([“some sql”, param1,  
param2])

Fred

(tip for next time: describe what errors you get rather than just  
saying it isn’t working)

---

<div class="post-metadata">

**Author:** ![dmarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dmarc](https://www.ruby-forum.com/u/dmarc)\
**Post date:** [April 27, 2009, 2:36am UTC](https://www.ruby-forum.com/t/find-by-sql/166514/3 "2009-04-27T02:36:52Z")

</div>

In case any more help is needed, what Fred is saying is that the  
question mark is not going to be replaced by anything because you are  
using a string and not an array for your find\_by\_sql. When the code  
runs the question mark will stay and the SQL engine will probably  
either find nothing because ‘elevators.booked\_date’ will never equals  
‘?’ or will blow up.

For your code to work you would need something like this (check the  
[and] ):

…find\_by\_sql([“select name from elevator\_timeslots WHERE NOT EXISTS  
(select elevator\_timeslot\_id from elevators where  
elevators.elevator\_timeslot\_id = elevator\_timeslots.id and  
elevators.booked\_date = ?)”, params[:booked\_date]])

Now you have an array (between the square brackets) and the value of  
the second element (params[:booked\_date]) will replace your question  
mark.

Pepe

On Apr 26, 11:48 am, Darren S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dmarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dmarc](https://www.ruby-forum.com/u/dmarc)\
**Post date:** [April 27, 2009, 5:58am UTC](https://www.ruby-forum.com/t/find-by-sql/166514/4 "2009-04-27T05:58:35Z")

</div>

Guys thanks so much for all your help. The sql is working great.

I really do appreciate your prompt replies.

Thanks, so much once again.
