Find syntax problem noob problem

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.

Still gives “undefined method `sguser_id’ for #Array:0xb6896af0

From: [email protected]
[mailto:[email protected]] On Behalf Of bala kishore
pulicherla
Sent: Saturday, August 30, 2008 6:17 PM
To: [email protected]
Subject: [Rails] Re: find syntax problem noob problem

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

hope it will work…

:slight_smile: Bala

On Sat, Aug 30, 2008 at 3:31 PM, tyliong [email protected] 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.

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

hope it will work…

:slight_smile: Bala

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?

:o

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

On Aug 30, 11:01 am, tyliong [email protected] 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

On Aug 31, 5:00 pm, “Tan YL” [email protected] 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

short form:

<%= params[:id] %>

:wink:

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. [email protected]