# Rails find condition that references an array generates error unless debugger is on

**URL:** <https://www.ruby-forum.com/t/rails-find-condition-that-references-an-array-generates-error-unless-debugger-is-on/142979>\
**Category:** Rails\
**Created:** [July 14, 2008, 5:57am UTC](https://www.ruby-forum.com/t/rails-find-condition-that-references-an-array-generates-error-unless-debugger-is-on/142979 "2008-07-14T05:57:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [July 14, 2008, 5:57am UTC](https://www.ruby-forum.com/t/rails-find-condition-that-references-an-array-generates-error-unless-debugger-is-on/142979/1 "2008-07-14T05:57:36Z")

</div>

I am getting inconsistent results when trying to pass an array as a  
find condition (using SQL WHERE…IN…). My code generates a  
StatementInvalid error unless execution is interrupted by the  
debugger, in which case the array is passed as expected (when  
execution resumes after issuing the ‘cont’ command).

#application.rb  
def current\_organisation\_units  
current\_user.organisation\_unit\_contacts.map {|o|  
[o.organisation\_unit\_id]}

# debugger # When this line is commented out, execution fails;

when this line is enabled, execution succeeds.  
end

#people\_controller.rb  
def index  
@people = Person.find(:all, :conditions =\>  
[“clients.organisation\_unit\_id IN (?)”,  
current\_organisation\_units], :include =\> :clients)  
end

The error (note the DBMS is Postgres):  
ActiveRecord::StatementInvalid (PGError: ERROR: invalid input syntax  
for integer: "—

- 1

The SQL that is generated:  
…WHERE (clients.organisation\_unit\_id IN (E’—

- 1  
')

With the debugger line in application.rb enabled, the following SQL is  
generated:  
…WHERE (clients.organisation\_unit\_id IN (1))  
This works as expected.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [July 14, 2008, 7:39am UTC](https://www.ruby-forum.com/t/rails-find-condition-that-references-an-array-generates-error-unless-debugger-is-on/142979/2 "2008-07-14T07:39:09Z")

</div>

> def index  
> @people = Person.find(:all, :conditions =\>  
> [“clients.organisation\_unit\_id IN (?)”,  
> current\_organisation\_units], :include =\> :clients)  
> end
> 
> The error (note the DBMS is Postgres):  
> ActiveRecord::StatementInvalid (PGError: ERROR: invalid input syntax  
> for integer: "—
> 
> - 1

the error above want say that current\_organisation\_units is saved in  
YAML version, current\_organisation\_units is array if it is saved in  
DATABASE it will be YAML version like "— 1 " but if it is parameter  
array, it will be string “1”, the matter now you should convert  
current\_organisation\_units to array back.  
try using:

YAML.load(current\_organisation\_units)

I dont know “current\_organisation\_units” come from. is it parameter or  
from table. so i only argue like above. I am not sure that if your array  
contains many element, your conditions in .find will work.

just come back if you get error. I hope my opinion can help you.

Reinhart  
blog : [http://teapoci.blogpsot.com](http://teapoci.blogpsot.com)  
YahooMessenger: sxapril

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [July 16, 2008, 4:31am UTC](https://www.ruby-forum.com/t/rails-find-condition-that-references-an-array-generates-error-unless-debugger-is-on/142979/3 "2008-07-16T04:31:05Z")

</div>

Thanks for you reply. Current\_organisation\_units is a method (not from  
a table) that uses current\_user (from the restful\_authentication  
plugin). My main issue is that the behaviour is correct if I interrupt  
execution with the debugger, but incorrect without the debugger.  
Although it would be interesting to find out what the debugger does  
that causes this difference, I’m mostly concerned with getting my code  
working without the debugger, of course.

On Jul 14, 5:39 pm, Rails T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [July 16, 2008, 10:16am UTC](https://www.ruby-forum.com/t/rails-find-condition-that-references-an-array-generates-error-unless-debugger-is-on/142979/4 "2008-07-16T10:16:48Z")

</div>

On 16 Jul 2008, at 03:30, Chris B. wrote:

> Thanks for you reply. Current\_organisation\_units is a method (not from  
> a table) that uses current\_user (from the restful\_authentication  
> plugin). My main issue is that the behaviour is correct if I interrupt  
> execution with the debugger, but incorrect without the debugger.  
> Although it would be interesting to find out what the debugger does  
> that causes this difference, I’m mostly concerned with getting my code  
> working without the debugger, of course.

well having debugger on the last line of a function will change the  
return value of the function (since a functions return value is the  
value of the last statement). Why it changes it in any particular way  
is anyone’s guess

But your problem here is  
current\_user.organisation\_unit\_contacts.map {|o|  
[o.organisation\_unit\_id]}  
This will return something like [[1], [23], [485]] rather than  
[1,23,485]

Fred

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [July 22, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/rails-find-condition-that-references-an-array-generates-error-unless-debugger-is-on/142979/5 "2008-07-22T23:51:00Z")

</div>

Whoops. Thanks for spotting this for me, Fred. It’s working now that  
I’ve changed that line to:  
current\_user.organisation\_unit\_contacts.map {|o|  
o.organisation\_unit\_id}

On Jul 16, 8:15 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
