# Find syntax problem noob problem

**URL:** <https://www.ruby-forum.com/t/find-syntax-problem-noob-problem/147290>\
**Category:** Rails\
**Created:** [August 30, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/find-syntax-problem-noob-problem/147290 "2008-08-30T12:02:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [August 30, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/find-syntax-problem-noob-problem/147290/1 "2008-08-30T12:02:40Z")

</div>

Hi,

I need to know why

\<%= SglineItem.find(1).sguser\_id %\>

works but

\<%= SglineItem.find(:all, :conditions =\> “sguser\_id =  
#{params[:id]}”).sguser\_id %\>

does not. the second gives sguser\_id is not valid.

---

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [August 30, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/find-syntax-problem-noob-problem/147290/2 "2008-08-30T12:28:58Z")

</div>

Still gives “undefined method `sguser\_id’ for #Array:0xb6896af0”

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of bala kishore  
pulicherla  
Sent: Saturday, August 30, 2008 6:17 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Rails] Re: find syntax problem noob problem

try  
\<%= SglineItem.find(:all, :conditions =\> [‘sguser\_id =?’, params[:id] ]  
).sguser\_id %\>

hope it will work…

🙂 Bala

On Sat, Aug 30, 2008 at 3:31 PM, tyliong [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Hi,

I need to know why

\<%= SglineItem.find(1).sguser\_id %\>

works but

\<%= SglineItem.find(:all, :conditions =\> “sguser\_id =  
#{params[:id]}”).sguser\_id %\>

does not. the second gives sguser\_id is not valid.

---

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [August 30, 2008, 12:17pm UTC](https://www.ruby-forum.com/t/find-syntax-problem-noob-problem/147290/3 "2008-08-30T12:17:52Z")

</div>

try  
\<%= SglineItem.find(:all, :conditions =\> [‘sguser\_id =?’, params[:id] ]  
).sguser\_id %\>

hope it will work…

🙂 Bala

---

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [August 31, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/find-syntax-problem-noob-problem/147290/4 "2008-08-31T18:00:55Z")

</div>

Yeah this was what I thought. The finds are two different things. How do  
I  
get it to return a record instead of an array while looking for a value  
not  
in the :id coloumn Fedrick?

---

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [August 30, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/find-syntax-problem-noob-problem/147290/5 "2008-08-30T12:33:39Z")

</div>

:o

try  
\<% sgitem = SglineItem.find(:all, :conditions =\> [‘sguser\_id =?’,  
params[:id] ] ).sguser\_id %\>  
\<% for item in sgitem %\>  
\<%= item %\>  
\<%end %\>

---

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [August 30, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/find-syntax-problem-noob-problem/147290/6 "2008-08-30T13:54:58Z")

</div>

On Aug 30, 11:01 am, tyliong [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> does not. the second gives sguser\_id is not valid.

because find(1) (or find :first, … ) return a single record from the  
database.  
find :all, returns an array. that array does not have a sguser\_id  
method (even if the things in it do).

Fred

---

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [August 31, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/find-syntax-problem-noob-problem/147290/7 "2008-08-31T18:17:18Z")

</div>

On Aug 31, 5:00 pm, “Tan YL” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yeah this was what I thought. The finds are two different things. How do I  
> get it to return a record instead of an array while looking for a value not  
> in the :id coloumn Fedrick?

use find :first instead of find :all (or just take the array and  
select the first item in it) (and of course you can use dynamic  
finders, find\_by\_xxx is analogous to find :first)

Fred

---

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [August 31, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/find-syntax-problem-noob-problem/147290/8 "2008-08-31T19:31:38Z")

</div>

short form:

\<%= params[:id] %\>

😉

other than that:

\<%= SglineItem.find(:first, :conditions =\> [“sguser\_id = ?”,  
params[:id]]).sguser\_id %\>

or

\<%= SglineItem.find\_by\_sguser\_id(params[:id]).sguser\_id %\>

you should be careful though because these queries could return nil if  
there’s no record with the given id in the db.  
also, if you use conditions you should use the array syntax (like  
above) to prevent sql injection.

cheers

gerold

On 31 Aug., 18:16, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
