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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 16, 2007, at 12:56 AM, john95127 wrote:> 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.@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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 16 Nov 2007, at 08:56, john95127 wrote:> > 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.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Nov 16, 2:54 am, Greg Willits <li...-0Bv1hcaDFPRk211Z5VL+QA@public.gmane.org> wrote:> On Nov 16, 2007, at 12:56 AM, john95127 wrote: > > > 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. > > @variety is a collection of multiple records. > > use @variety[0] to show the first one. > > Look into "collections" for viewing each record. > > -- w (www.railsdev.ws)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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> > > > > On Nov 16, 2:54 am, Greg Willits <li...-0Bv1hcaDFPRk211Z5VL+QA@public.gmane.org> wrote: >> On Nov 16, 2007, at 12:56 AM, john95127 wrote: >> >>> 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. >> >> @variety is a collection of multiple records. >> >> use @variety[0] to show the first one. >> >> Look into "collections" for viewing each record. >> >> -- w (www.railsdev.ws) > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---