SQL result to String conversion

Hello All,

I am very new to Ruby on Rails and I have a quick question for you all,
which is as follows:

I have this following line of code in an partial that renders itself on
the posts/index view:
Created By (User): <%= User.find(:all, :select => ‘name’, :conditions =>
[“id = ?”, 1]) %>
(I am hard-coding that ‘1’ there)

Now, when I run the server; the above line of code displays just the ‘#’
symbol… nothing else (no name is displayed :frowning: ):
Created By (User): #

I have the following tables: User, Post & Comment. For your reference, I
am also pasting here the contents of my ‘User’ table that I got from my
console:

User.find(:all)
=> [#<User id: 1, name: “Ron”, created_at: “2010-03-15 02:45:28”,
updated_at: “2010-03-15 02:45:28”>, #<User id: 2, name: “David”,
created_at: “2010-03-15 21:13:22”, updated_at: “2010-03-15 21:13:22”>,
#<User id: 3, name: “Tom”, created_at: “2010-03-15 21:38:31”,
updated_at: “2010-03-15 21:38:31”>, #<User id: 4, name: “John”,
created_at: “2010-03-15 21:38:43”, updated_at: “2010-03-15 21:38:43”>,
#<User id: 5, name: “Harry”, created_at: “2010-03-15 21:38:50”,
updated_at: “2010-03-15 21:38:50”>, #<User id: 6, name: “Martin”,
created_at: “2010-03-15 21:38:57”, updated_at: “2010-03-15 21:38:57”>]

Can you please let me me know if I am missing something here or if I am
doing anything wrong?

On Mar 16, 5:41 pm, Rohit S. [email protected] wrote:

Now, when I run the server; the above line of code displays just the ‘#’
symbol… nothing else (no name is displayed :frowning: ):
Created By (User): #

That’s because all that is being stuck in the view is something like

#<User id: 1, name: “Ron”, created_at: “2010-03-15 02:45:28”,
updated_at: “2010-03-15 02:45:28”>

Which isn’t legal html (you can verify this by viewing the html source
in your browser)

Fred

Thanks Fred… when I view d page source… I see something like this:
Created By (User): #User:0x1042bbec0

I guess this is hex representation of the memory point to the following
object <User id: 1, name: “Ron”, created_at: “2010-03-15 02:45:28”,

updated_at: “2010-03-15 02:45:28”>

Can you please advice me a way to extract just the name from there?

Frederick C. wrote:

On Mar 16, 5:41�pm, Rohit S. [email protected] wrote:

Now, when I run the server; the above line of code displays just the ‘#’
symbol… nothing else (no name is displayed :frowning: ):
Created By (User): #

That’s because all that is being stuck in the view is something like

#<User id: 1, name: “Ron”, created_at: “2010-03-15 02:45:28”,
updated_at: “2010-03-15 02:45:28”>

Which isn’t legal html (you can verify this by viewing the html source
in your browser)

Fred

On 16 March 2010 19:33, Rohit S. [email protected] wrote:

Can you please advice me a way to extract just the name from there?

This will give you what you want:
<%= User.find(:all, :select => ‘name’, :conditions => [“id = ?”,
1]).name %>

…but there are so many poor and bad-practice things going on in that
line… It’s great that you’ve got as far as you have - to have
working models and views - but there’s some large steps you need to
take that are covered well in lots of online references and in any of
the books that deal with getting started with Ruby/Rails.
I’d suggest visiting them and getting much more comfortable with the
basics to help you to get as quickly as possible to a position where
you will be able to produce useful code.

On 16 March 2010 20:28, Rohit S. [email protected] wrote:

Thanks a lot Michael… but this (<%= User.find(:all, :select =>
‘name’, :conditions => [“id = ?”, 1]).name %>) did not work.

It gave me the following ‘NoMethodError’ - undefined method `name’ for
[#<User name: “Ron”>]:Array

Sorry… “find :all” returns an array… my bad. Use this instead:

<%= User.find(:all, :select => ‘name’, :conditions => [“id = ?”,
1]).first.name %>

That worked… thanks a lot Michael :slight_smile:

Thanks a lot Michael… but this (<%= User.find(:all, :select =>
‘name’, :conditions => [“id = ?”, 1]).name %>) did not work.

It gave me the following ‘NoMethodError’ - undefined method `name’ for
[#<User name: “Ron”>]:Array

:frowning: