Find results help!

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

On 6/30/07, incoroneta [email protected] 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

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

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

<%= @users.inspect %>

On 6/30/07, incoroneta [email protected] 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

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

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

On 6/30/07, incoroneta [email protected] 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

On 6/30/07, incoroneta [email protected] 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 %>

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

On 6/30/07, incoroneta [email protected] 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?

yes it is in ActiveRecord class.

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

thanks!!