# find\_by\_sql

**URL:** <https://discuss.rubyonrails.org/t/find-by-sql/41957>\
**Category:** rubyonrails-talk\
**Created:** [April 26, 2009, 3:48pm UTC](https://discuss.rubyonrails.org/t/find-by-sql/41957 "2009-04-26T15:48:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [April 26, 2009, 3:48pm UTC](https://discuss.rubyonrails.org/t/find-by-sql/41957/1 "2009-04-26T15: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:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 26, 2009, 8:20pm UTC](https://discuss.rubyonrails.org/t/find-by-sql/41957/2 "2009-04-26T20:20:14Z")

</div>

> 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:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [April 27, 2009, 12:36am UTC](https://discuss.rubyonrails.org/t/find-by-sql/41957/3 "2009-04-27T00:36:08Z")

</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

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [April 27, 2009, 3:58am UTC](https://discuss.rubyonrails.org/t/find-by-sql/41957/4 "2009-04-27T03: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.
