Forum: Ruby on Rails find results help!!!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D97d7978720612c63dc899ad4b25b5b3?d=identicon&s=25 incoroneta (Guest)
on 2007-06-29 16:07
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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-06-29 16:13
(Received via mailing list)
On 6/30/07, incoroneta <rails-mailing-list@andreas-s.net> 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
D97d7978720612c63dc899ad4b25b5b3?d=identicon&s=25 incoroneta (Guest)
on 2007-06-29 16:35
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
2da252cc1d2fb3701126171831b50a65?d=identicon&s=25 Dave Coleman (pezdude)
on 2007-06-29 16:35
and to find out what your AR object looks like, you can do:

<pre>
<%= @users.inspect %>
</pre>
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-06-29 16:40
(Received via mailing list)
On 6/30/07, incoroneta <rails-mailing-list@andreas-s.net> 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
D97d7978720612c63dc899ad4b25b5b3?d=identicon&s=25 incoroneta (Guest)
on 2007-06-29 16:54
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 %>
<p>
  <b><%= column.human_name %>:</b> <%=h @user.send(column.name) %>
</p>
<% 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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-06-29 17:03
(Received via mailing list)
On 6/30/07, incoroneta <rails-mailing-list@andreas-s.net> 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 %>
  <p>
    <b><%= column.human_name %>:</b> <%=h user.send(column.name) %>
  </p>
  <% 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
D97d7978720612c63dc899ad4b25b5b3?d=identicon&s=25 incoroneta (Guest)
on 2007-06-29 17:10
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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-06-29 17:27
(Received via mailing list)
On 6/30/07, incoroneta <rails-mailing-list@andreas-s.net> 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 %>
    <p>
      <b><%= column.human_name %>:</b> <%=h @user.send(column.name) %>
    </p>
    <% end %>

    <%= link_to 'Edit', :action => 'edit', :id => @user %> |
    <%= link_to 'Back', :action => 'list' %>
  <% end %>
<% end %>
D97d7978720612c63dc899ad4b25b5b3?d=identicon&s=25 incoroneta (Guest)
on 2007-06-29 17:43
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!!!!
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-06-29 17:57
(Received via mailing list)
On 6/30/07, incoroneta <rails-mailing-list@andreas-s.net> 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?
D97d7978720612c63dc899ad4b25b5b3?d=identicon&s=25 incoroneta (Guest)
on 2007-06-29 18:02

yes it is in ActiveRecord class.

i'll take a look and see what is happening...
i'll let u know

thanks!!
This topic is locked and can not be replied to.