bohara wrote:> ===============> def edit > @product = Product.find(params[:id]) > @categories = Category.find_all > @productdetails = Productdetail.find(:all, :conditions => > ["product_id = 3"]) > End > ===============Change your third line to this: @productdetails = Productdetail.find(:first, :conditions => ["product_id = ?", @product.id]) However, a much easier way of doing this is to define the relationship between the two models: # models/Product.rb class Product < ActiveRecord::Base belongs_to :productdetail end # models/Productdetail.rb class Productdetail < ActiveRecord::Base has_one :product end Then, all you have to do is this: @product = Product.find(params[:id], :include => [:productdetail]) And now you have it all in one query... -- Posted via http://www.ruby-forum.com/.
Hello all, I have two tables "products" & "productdetails" in my productdetails table I have a foreign key field called "product_id" If I am editing product number "3" and run the following I can get and display the detail information. ===============def edit @product = Product.find(params[:id]) @categories = Category.find_all @productdetails = Productdetail.find(:all, :conditions => ["product_id = 3"]) End =============== How do make that "3" be the product id from the products table dynamically - so it always represents the product I am currently working on. I''ve tried some different things but nothing is working. Any help is appreciated. Thanks, Beau
Thanks, that works great. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Brad Daily Sent: Thursday, December 01, 2005 12:40 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] Re: Active record question bohara wrote:> ===============> def edit > @product = Product.find(params[:id]) > @categories = Category.find_all > @productdetails = Productdetail.find(:all, :conditions => > ["product_id = 3"]) End ===============Change your third line to this: @productdetails = Productdetail.find(:first, :conditions => ["product_id = ?", @product.id]) However, a much easier way of doing this is to define the relationship between the two models: # models/Product.rb class Product < ActiveRecord::Base belongs_to :productdetail end # models/Productdetail.rb class Productdetail < ActiveRecord::Base has_one :product end Then, all you have to do is this: @product = Product.find(params[:id], :include => [:productdetail]) And now you have it all in one query... -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails