# Building a FAQ

**URL:** <https://www.ruby-forum.com/t/building-a-faq/94464>\
**Category:** Rails\
**Created:** [May 4, 2006, 2:46am UTC](https://www.ruby-forum.com/t/building-a-faq/94464 "2006-05-04T02:46:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JesSSSSs\_Dugarte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@JesSSSSs\_Dugarte](https://www.ruby-forum.com/u/JesSSSSs_Dugarte)\
**Post date:** [May 4, 2006, 2:46am UTC](https://www.ruby-forum.com/t/building-a-faq/94464/1 "2006-05-04T02:46:45Z")

</div>

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 %\>

### category.name
 \<% for faq in @category.faqs %\> 

**\<%= faq.question %\>**

\<%= faq.answer %\>

 \<% 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.-

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Dugarte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@JesSSSSs\_Dugarte](https://www.ruby-forum.com/u/JesSSSSs_Dugarte)\
**Post date:** [May 4, 2006, 3:20am UTC](https://www.ruby-forum.com/t/building-a-faq/94464/2 "2006-05-04T03:20:13Z")

</div>

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

> End  
> end
> 
> — View —  
> \<% for category in categories %\>  
> \<% categories.each do |category| %\>  
> is much more Ruby-like BTW.

> ### category.name
> \<% for faq in category.faqs %\>

There is no 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.

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Dugarte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@JesSSSSs\_Dugarte](https://www.ruby-forum.com/u/JesSSSSs_Dugarte)\
**Post date:** [May 4, 2006, 1:48pm UTC](https://www.ruby-forum.com/t/building-a-faq/94464/3 "2006-05-04T13:48:06Z")

</div>

Kian wrote:

> JesÃºs Dugarte \<jdugarte@…\> writes:
> 
> > — Controler —  
> > 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 categories %\>  
> > \<% 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 category.faqs %\>  
> > There is no category at this point, just category

Sorry, 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.-
