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?
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)
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.
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
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