# Newb - variable returns # sign?

**URL:** <https://www.ruby-forum.com/t/newb-variable-returns-sign/120372>\
**Category:** Rails\
**Created:** [November 16, 2007, 10:05am UTC](https://www.ruby-forum.com/t/newb-variable-returns-sign/120372 "2007-11-16T10:05:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![john95127](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@john95127](https://www.ruby-forum.com/u/john95127)\
**Post date:** [November 16, 2007, 10:05am UTC](https://www.ruby-forum.com/t/newb-variable-returns-sign/120372/1 "2007-11-16T10:05:42Z")

</div>

Hi Gurus-

I want to return the variety from the species table based on the  
active record link to inventory table

class Species \< ActiveRecord::Base  
has\_many :inventories  
end

class Inventory \< ActiveRecord::Base  
belongs\_to :species (this is the singular and the plural, so that’s  
OK)  
end

My inventory\_controller looks like:

def show  
@inventory = Inventory.find(params[:id])  
@variety = Species.find(params[:species\_id])  
end

and in the inventory show view:

\<%= @variety %\>

my out put shows all the fields from the inventory but only a pound  
sign(#) where the @variety is at.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![john95127](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@john95127](https://www.ruby-forum.com/u/john95127)\
**Post date:** [November 16, 2007, 11:55am UTC](https://www.ruby-forum.com/t/newb-variable-returns-sign/120372/2 "2007-11-16T11:55:56Z")

</div>

On Nov 16, 2007, at 12:56 AM, john95127 wrote:

> sign(#) where the @variety is at.  
> @variety is a collection of multiple records.

use @variety[0] to show the first one.

Look into “collections” for viewing each record.

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![john95127](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@john95127](https://www.ruby-forum.com/u/john95127)\
**Post date:** [November 16, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/newb-variable-returns-sign/120372/3 "2007-11-16T17:22:35Z")

</div>

Keeping in mind that each inventory item has only species; if I  
wanted to display the variety (from species) while I was in the show  
view (of inventory) do I even need to set a variable? In SQL (my  
background) I would : “SELECT variety FROM species a, inventory b  
WHERE a.id = b.species\_id AND b.id = (the current record)” I just  
want to display the associated value from another table, Sorry for the  
simplistic questions, I have searched my book and this forum to no  
avail.

---

<div class="post-metadata">

**Author:** ![john95127](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@john95127](https://www.ruby-forum.com/u/john95127)\
**Post date:** [November 16, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/newb-variable-returns-sign/120372/4 "2007-11-16T12:02:58Z")

</div>

On 16 Nov 2007, at 08:56, john95127 wrote:

> class Inventory \< ActiveRecord::Base
> 
> and in the inventory show view:
> 
> \<%= @variety %\>
> 
> my out put shows all the fields from the inventory but only a pound  
> sign(#) where the @variety is at.

When you do \<%= foo %\>, it just tries to turn foo into a string.  
For your basic activerecord model, that gives you something like  
#Question:0x17ce158, so I’m not surprised that the browser only  
displays a #. Depending on what you’re trying to do you may want to  
define to\_s on your model, show each property individually or if  
you’re just checking that @variety is being set properly you can use \<  
%= debug @variety %\>

Fred

---

<div class="post-metadata">

**Author:** ![john95127](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@john95127](https://www.ruby-forum.com/u/john95127)\
**Post date:** [November 16, 2007, 6:17pm UTC](https://www.ruby-forum.com/t/newb-variable-returns-sign/120372/5 "2007-11-16T18:17:46Z")

</div>

On 16 Nov 2007, at 16:22, john95127 wrote:

> Keeping in mind that each inventory item has only species; if I  
> wanted to display the variety (from species) while I was in the show  
> view (of inventory) do I even need to set a variable? In SQL (my  
> background) I would : “SELECT variety FROM species a, inventory b  
> WHERE a.id = b.species\_id AND b.id = (the current record)” I just  
> want to display the associated value from another table, Sorry for the  
> simplistic questions, I have searched my book and this forum to no  
> avail.

The only problem you’re having is that the output from \<%= @variety %\>  
is not html friendly. if you were to look at the raw page source you’d  
see something like #\<Variety: #0x123465\>.  
Replace it with something else (see my previous email for  
suggestions). and you should be fine

Fred
