Hello, I''m trying to join 2 tables with ruby on rails 3.2. rails generate model product name:string rails generate model price product_id:integer price:integer date:datetime product.rb: class Product < ActiveRecord::Base has_many :prices end price.rb: class Price < ActiveRecord::Base belongs_to :product end What is the most efficient way to get all products including the latest price into the View. Some tries didnt work for this: @test = Product.includes(:prices).all I didnt find anything about the prices in @test. Thanks for help! Fabian -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Thu, Jan 31, 2013 at 9:52 PM, Fabian Peter <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > > I''m trying to join 2 tables with ruby on rails 3.2. > > rails generate model product name:string > rails generate model price product_id:integer price:integer > date:datetime > > product.rb: > class Product < ActiveRecord::Base > has_many :prices > end > > price.rb: > class Price < ActiveRecord::Base > belongs_to :product > end > > What is the most efficient way to get all products including the latest > price into the View. > > Some tries didnt work for this: > > @test = Product.includes(:prices).all > I didnt find anything about the prices in @test. >@test contains an array of products. to get the prices, try @test.each do |product| product.prices.each do |price| p price.price end end> > Thanks for help! > > Fabian > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 31 January 2013 13:55, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Thu, Jan 31, 2013 at 9:52 PM, Fabian Peter <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> Hello, >> >> I''m trying to join 2 tables with ruby on rails 3.2. >> >> rails generate model product name:string >> rails generate model price product_id:integer price:integer >> date:datetime >> >> product.rb: >> class Product < ActiveRecord::Base >> has_many :prices >> end >> >> price.rb: >> class Price < ActiveRecord::Base >> belongs_to :product >> end >> >> What is the most efficient way to get all products including the latest >> price into the View. >> >> Some tries didnt work for this: >> >> @test = Product.includes(:prices).all >> I didnt find anything about the prices in @test. > > > @test contains an array of products. to get the prices, try > > @test.each do |product| > product.prices.each do |price| > p price.price > end > endBut you don''t need .includes, @test = Product.all will work fine (though I would prefer @products =Product.all since that is what they are). If you inspect @test you will not see the prices, but as soon as you ask for the prices it will fetch them. Such is the magic or Rails. I am sure readers here are bored with me suggesting to beginners that they work right through a good tutorial such as railstutorial.org, which is free to use online, but it is definitely a good idea as it would give you a good introduction to the basics of rails. Also look through the Rails Guides. For some reason I am rarely thanked for offering this advice. :( Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.