# Find results help!

**URL:** <https://www.ruby-forum.com/t/find-results-help/108168>\
**Category:** Rails\
**Created:** [June 29, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/find-results-help/108168 "2007-06-29T16:07:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/1 "2007-06-29T16:07:22Z")

</div>

Hi,

i’m trying to get the results from find(). The code of the find is:

@user = User.find(:all, :conditions =\> [“user\_id = ?”, val])

i’ve checked it with the function @user.size and it returns me one  
record.

The problem is that inside @user i see only the character “#” and when i  
try to print some value of the record rails for example @user.nome,  
rails return me that error:

undefined method `nome’ for #Array:0xb68ba5e0

but nome is one column of the table!!!

Can somebody help me???

thanks

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/2 "2007-06-29T16:13:37Z")

</div>

On 6/30/07, incoroneta [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanks  
> When you use find with the :all condition you are always returned an  
> array.

The returned array is a collection of Active Record objects or the  
resulting  
models from the database.  
@users = User.find(:all, :conditions =\> [“user\_id = ?”, val])

To get the first  
@users.first

To get the last  
@users.last

To go through each of them

@users.each do |user|  
do\_stuff  
end

If your only after the first one then instead of using the :all option  
user  
the :first option. Instead of retuurning an array this will return an  
individual record as an ActiveRecord instance.

HTH  
Daniel

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/3 "2007-06-29T16:35:50Z")

</div>

Hi Daniel,

i’ve tried both to use the first condition inside the find like this

@user = User.find(:first, :conditions =\> [“user\_id = ?”, val])

and to take the result like

@user.first

but i’ve still the problem…

The value inside @user is always #

thanks

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/4 "2007-06-29T16:35:59Z")

</div>

and to find out what your AR object looks like, you can do:

```
<%= @users.inspect %>
```

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 4:40pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/5 "2007-06-29T16:40:39Z")

</div>

On 6/30/07, incoroneta [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @user.first
> 
> but i’ve still the problem…
> 
> The value inside @user is always #
> 
> thanks

As Dave said check what is in your @user variable with @user.inspect

Can you post the actual controller code, and the view code please. It’s  
a  
little hard to guess what’s happening

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/6 "2007-06-29T16:54:04Z")

</div>

i check the AR with @user.inspect ant the result is ok.

the controller code is

def show  
val=params[:id]  
@users = User.find(:all, :conditions =\> [“user\_id = ?”, val])

end

and the view code is

\<% for column in User.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h @user.send(column.name) %\>

\<% end %\>

\<%= link\_to ‘Edit’, :action =\> ‘edit’, :id =\> @user %\> |  
\<%= link\_to ‘Back’, :action =\> ‘list’ %\>

the error  
undefined method `nome’ for #Array:0xb68ba5e0  
is given when its called @user.send(column.name) or also if you try to  
do  
flash[:notice] = @user.nome

thanks

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/7 "2007-06-29T17:10:00Z")

</div>

hi Daniel,

i tried and i get this error

undefined method `find\_all\_by\_id’ for User:Class

i’ve to include some libraries??

thanks

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/8 "2007-06-29T17:03:42Z")

</div>

On 6/30/07, incoroneta [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> \<%= link\_to ‘Back’, :action =\> ‘list’ %\>  
> thanks  
> def show

@users = User.find(:all, :conditions =\> [“user\_id = ?”, params[:id]  
])

end

You can change this to

def show  
@users = User.find\_all\_by\_id( params[:id] )  
end

It will be auto sanitized for you.

You need to wrap up the view to run for each user. You’re passing it an  
array so you need to go through each element of it.

\<% @users.each do |user| %\>  
\<% for column in User.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h user.send(column.name) %\>

 \<% end %\>

\<%= link\_to ‘Edit’, :action =\> ‘edit’, :id =\> user %\> |  
\<%= link\_to ‘Back’, :action =\> ‘list’ %\>  
\<% end %\>

That should do the trick for you. Please let me know if you don’t get  
anything there.

HTH  
Daniel

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/9 "2007-06-29T17:27:44Z")

</div>

On 6/30/07, incoroneta [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanks
> 
> –

mmm That’s strange. I’m able to use it. It’s a standard method.

Have you had a look in console to see what’s going on? This is really  
helpful.

Alternatively you could use a simple find\_by\_id

User.find\_by\_id( params[:id] )

But this changes it a bit. You don’t need to go through an array now  
since  
it will provide either a user record or nil

The controller action becomes  
def show  
@user = User.find\_by\_id( params[:id] )  
end

And the view  
\<% unless @user.nil? %\>  
\<% for column in User.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h @user.send(column.name) %\>

  
\<% end %\>

```
<%= link_to 'Edit', :action => 'edit', :id => @user %> |
<%= link_to 'Back', :action => 'list' %>

```

\<% end %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/10 "2007-06-29T17:43:34Z")

</div>

i’ve the same error…

undefined method `find\_by\_id’ for User:Class

What do u mean for have a look in console??

how can i have a look in console to see what’s going on???

really thanks for the help!!!

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/11 "2007-06-29T17:57:47Z")

</div>

On 6/30/07, incoroneta [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> really thanks for the help!!!

In your rails directory type

ruby script/console

This will give you a command prompt. You can type ruby in that will be  
executed in the rails environment

so  
user = User.find( :first )  
should return the first user in the database.

Your user is an ActiveRecord class right?

---

<div class="post-metadata">

**Author:** ![incoroneta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@incoroneta](https://www.ruby-forum.com/u/incoroneta)\
**Post date:** [June 29, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/find-results-help/108168/12 "2007-06-29T18:02:58Z")

</div>

yes it is in ActiveRecord class.

i’ll take a look and see what is happening…  
i’ll let u know

thanks!!
