# SQL result to String conversion

**URL:** <https://www.ruby-forum.com/t/sql-result-to-string-conversion/185241>\
**Category:** Rails\
**Created:** [March 16, 2010, 6:41pm UTC](https://www.ruby-forum.com/t/sql-result-to-string-conversion/185241 "2010-03-16T18:41:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@rohits](https://www.ruby-forum.com/u/rohits)\
**Post date:** [March 16, 2010, 6:41pm UTC](https://www.ruby-forum.com/t/sql-result-to-string-conversion/185241/1 "2010-03-16T18:41:05Z")

</div>

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 ☹ ):  
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?

---

<div class="post-metadata">

**Author:** ![rohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@rohits](https://www.ruby-forum.com/u/rohits)\
**Post date:** [March 16, 2010, 8:12pm UTC](https://www.ruby-forum.com/t/sql-result-to-string-conversion/185241/2 "2010-03-16T20:12:53Z")

</div>

On Mar 16, 5:41 pm, Rohit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now, when I run the server; the above line of code displays just the ‘#’  
> symbol… nothing else (no name is displayed ☹ ):  
> 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

---

<div class="post-metadata">

**Author:** ![rohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@rohits](https://www.ruby-forum.com/u/rohits)\
**Post date:** [March 16, 2010, 8:33pm UTC](https://www.ruby-forum.com/t/sql-result-to-string-conversion/185241/3 "2010-03-16T20:33:53Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Now, when I run the server; the above line of code displays just the ‘#’  
> > symbol… nothing else (no name is displayed ☹ ):  
> > 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

---

<div class="post-metadata">

**Author:** ![rohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@rohits](https://www.ruby-forum.com/u/rohits)\
**Post date:** [March 16, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/sql-result-to-string-conversion/185241/4 "2010-03-16T20:50:21Z")

</div>

On 16 March 2010 19:33, Rohit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![rohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@rohits](https://www.ruby-forum.com/u/rohits)\
**Post date:** [March 16, 2010, 9:49pm UTC](https://www.ruby-forum.com/t/sql-result-to-string-conversion/185241/5 "2010-03-16T21:49:28Z")

</div>

On 16 March 2010 20:28, Rohit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 %\>

---

<div class="post-metadata">

**Author:** ![rohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@rohits](https://www.ruby-forum.com/u/rohits)\
**Post date:** [March 16, 2010, 11:59pm UTC](https://www.ruby-forum.com/t/sql-result-to-string-conversion/185241/6 "2010-03-16T23:59:35Z")

</div>

That worked… thanks a lot Michael 🙂

---

<div class="post-metadata">

**Author:** ![rohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@rohits](https://www.ruby-forum.com/u/rohits)\
**Post date:** [March 16, 2010, 9:28pm UTC](https://www.ruby-forum.com/t/sql-result-to-string-conversion/185241/7 "2010-03-16T21:28:44Z")

</div>

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

☹
