I''m new in Rails, and I''m building my first application. I''m trying to make a FAQ for this application, organized by product (questions about each product) and category (questions of different categories or topics), and there is something I can''t figure it out how to do. I have these tables: create_table "faqs" do |t| t.column "question", :text t.column "answer", :text t.column "product_id", :integer t.column "category_id", :integer t.column "position", :integer end create_table "products" do |t| t.column "name", :string t.column "description", :text end create_table "categories" do |t| t.column "name", :string t.column "description", :text t.column "position", :integer end And these models: class Product < ActiveRecord::Base has_many :faqs, :order => "category_id, position" has_many :categories, :through => :faqs, :select => ''DISTINCT categories.*'' end class Category < ActiveRecord::Base has_many :faqs, :order => "product_id, position" has_many :products, :through => :faqs, :select => ''DISTINCT products.*'' acts_as_list end class Faq < ActiveRecord::Base belongs_to :product belongs_to :category acts_as_list :scope => :category end And I''d like to do something like this: --- Controler --- class FaqsController < ApplicationController def show @categories = Product.find(1).categories End end --- View --- <% for category in @categories %> <h3>category.name</h3> <% for faq in @category.faqs %> <p><b><%= faq.question %></b></p> <p><%= faq.answer %></p> <% end %> <% end %> Is this possible? I''ve been working on it for several hours and I haven''t found a solution. Also I''d like to make a question available to all products by setting its product_id to 0. How could I hack into the "has_many :faqs" definition in the Product model to search, not only the questions assigned to the current product, but also all the questions with product_id == 0. Is there a way to do that? Thanks in advance, Jes?s Dugarte.-
Jes?s Dugarte <jdugarte@...> writes:> > I''m new in Rails, and I''m building my first application. I''m trying to make > a FAQ for this application, organized by product (questions about each > product) and category (questions of different categories or topics), and > there is something I can''t figure it out how to do. >...> > --- Controler --- > class FaqsController < ApplicationController > def show > <at> categories = Product.find(1).categoriesYou realize that this only finds the categories for the first product, not all categories in the database, right? It''s fine for testing, or if that''s what you want your app to do.> End > end > > --- View --- > <% for category in <at> categories %><% <at>categories.each do |category| %> is much more Ruby-like BTW.> <h3>category.name</h3> > <% for faq in <at> category.faqs %>There is no <at>category at this point, just category. Try instead: <% category.faqs.each |faq| %>> <p><b><%= faq.question %></b></p> > <p><%= faq.answer %></p> > <% end %> > <% end %>...> Also I''d like to make a question available to all products by setting its > product_id to 0. How could I hack into the "has_many :faqs" definition in > the Product model to search, not only the questions assigned to the current > product, but also all the questions with product_id == 0. Is there a way to > do that?Hmmm... You should probably set product_id to null instead of 0. You could specify a :finder_sql on the has_many. As in: has_many :faqs, :finder_sql => ''select faqs.* from faqs where product_id #{self.id} or product_id is null order by category_id, position'' But this seems like a hack. You might want to rethink your model a bit. See other comments inline above.
Kian wrote:> Jes?s Dugarte <jdugarte@...> writes: >> --- Controler --- >> <at> categories = Product.find(1).categories > You realize that this only finds the categories for the first product, > not all > categories in the database, right? It''s fine for testing, or if that''s > what you > want your app to do.Yes, I realize that. I only set it here like that to illustrate the case>> --- View --- >> <% for category in <at> categories %> > <% <at>categories.each do |category| %> > is much more Ruby-like BTW.I appreciate the note. I''m actually finding my way into writing code ?the Ruby way? :-)>> <% for faq in <at> category.faqs %> > There is no <at>category at this point, just categorySorry, that was my mistake, I know there wouldn''t be @category at that point, just category>> Also I''d like to make a question available to all products by setting its >> product_id to 0. How could I hack into the "has_many :faqs" definition in >> the Product model to search, not only the questions assigned to the current >> product, but also all the questions with product_id == 0. Is there a way to >> do that? > Hmmm... You should probably set product_id to null instead of 0. You > could > specify a :finder_sql on the has_many. As in: > has_many :faqs, :finder_sql => ''select faqs.* from faqs where > product_id > #{self.id} or product_id is null order by category_id, position'' > But this seems like a hack. You might want to rethink your model a bit.I thought of that use of the :finder_sql, but I didn?t find it very? ?elegant? perhaps? I was also trying to change the models, but I think the way they are now feels more ?natural?. What do you think? Is there any other easier way to model this case? Thanks, Jes?s.- -- Posted via http://www.ruby-forum.com/.