# Model Structure Advise

**URL:** <https://www.ruby-forum.com/t/model-structure-advise/61578>\
**Category:** Rails\
**Created:** [May 25, 2006, 5:22am UTC](https://www.ruby-forum.com/t/model-structure-advise/61578 "2006-05-25T05:22:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [May 25, 2006, 5:22am UTC](https://www.ruby-forum.com/t/model-structure-advise/61578/1 "2006-05-25T05:22:22Z")

</div>

Hi,

I’m starting to build a simple (?!) FAQ system, and I’d like some  
advice on setting up the model relationships.

I was going to go with a simple system of a Category table and a FAQ  
table (holding questions and answers) - where one FAQ belongs to one  
category.

That’s all nice - a simple has\_many / belongs\_to relationship. Then I  
decided to add another level - Area. So I could make the system work  
for a wider audience (IT area has it’s own categories and FAQ’s, Sales  
area has it’s own categories and FAQ’s, etc.)

I’m not sure how the best way to set this up - areas has\_many  
categories has\_many faqs, or areas has\_many faqs and categories  
has\_many faqs.

Given the page will probably have a menu at the top to select the  
area, then will display the categories and faqs (with searching, etc.)  
ie. For area X, I’d need to select the faqs in that area, or the faqs  
within a category of an area.

Any advice?

Thanks!

john

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [May 25, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/model-structure-advise/61578/2 "2006-05-25T17:47:55Z")

</div>

On 5/24/06, John T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I’m starting to build a simple (?!) FAQ system, and I’d like some  
> advice on setting up the model relationships.

…

> Any advice?

Hmm… just a hint? maybe?

jt

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [May 25, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/model-structure-advise/61578/3 "2006-05-25T18:14:58Z")

</div>

This isn’t a Rails thing… it’s basic relational database design,  
really.

Nobody can determine what you need better than you can. From the sound  
of it, though, you would be well-served by a nested (a/k/a  
“self-referential”) category table, so that what you call an “area” is a  
top-level category and the “categories” are children of a top-level, so  
that you’d have

Two good ways to set this up in Rails are acts\_as\_tree and single-table  
inheritance. Both are addressed in the Rails API docs. Acts\_as\_tree  
might be better for your purposes in the long run because it will allow  
you to add subcategories, sub-subcategories and so on with minimal fuss.

John T. wrote:

> Hi,
> 
> I’m starting to build a simple (?!) FAQ system, and I’d like some  
> advice on setting up the model relationships.
> 
> I was going to go with a simple system of a Category table and a FAQ  
> table (holding questions and answers) - where one FAQ belongs to one  
> category.
> 
> That’s all nice - a simple has\_many / belongs\_to relationship. Then I  
> decided to add another level - Area. So I could make the system work  
> for a wider audience (IT area has it’s own categories and FAQ’s, Sales  
> area has it’s own categories and FAQ’s, etc.)
> 
> I’m not sure how the best way to set this up - areas has\_many  
> categories has\_many faqs, or areas has\_many faqs and categories  
> has\_many faqs.
> 
> Given the page will probably have a menu at the top to select the  
> area, then will display the categories and faqs (with searching, etc.)  
> ie. For area X, I’d need to select the faqs in that area, or the faqs  
> within a category of an area.
> 
> Any advice?
> 
> Thanks!
> 
> john

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [May 25, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/model-structure-advise/61578/4 "2006-05-25T21:13:13Z")

</div>

> I’m not sure how the best way to set this up - areas has\_many  
> categories has\_many faqs, or areas has\_many faqs and categories  
> has\_many faqs.

class Area  
has\_many :categories  
end

class Category  
belongs\_to :area  
has\_many :answers  
end

class Answer  
belongs\_to :category  
end

# All answers from the first category in the IT area

## Area.find\_by\_name(“IT”).categories.first.answers

David Heinemeier H.  
[http://www.loudthinking.com](http://www.loudthinking.com) – Broadcasting Brain  
[http://www.basecamphq.com](http://www.basecamphq.com) – Online project management  
[http://www.backpackit.com](http://www.backpackit.com) – Personal information manager  
[http://www.rubyonrails.com](http://www.rubyonrails.com) – Web-application framework

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [May 25, 2006, 10:10pm UTC](https://www.ruby-forum.com/t/model-structure-advise/61578/5 "2006-05-25T22:10:21Z")

</div>

On 5/25/06, David Heinemeier H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :answers  
> end
> 
> class Answer  
> belongs\_to :category  
> end
> 
> # All answers from the first category in the IT area
> 
> Area.find\_by\_name(“IT”).categories.first.answers

Cool, thanks! That’s the way I was leaning - nice to have my thoughts  
verified by “the source” 🙂

jt

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [May 26, 2006, 11:44am UTC](https://www.ruby-forum.com/t/model-structure-advise/61578/6 "2006-05-26T11:44:52Z")

</div>

The mentioned approach in Rails will work fine for the domain specified  
by  
John.  
I have a similar problem which is little complicated as it turns out to  
be  
relationship between 3 entities.

Consider an Event Management system, which has following entities,

1. Event (an event in the system)
2. Organizer (an event is organized by a group of people, an organizer  
can  
be part of many events)
3. Roles An organizer has a role to play in the Event, An organizer can  
play more than one role inside an event, for e.g. he can be a approver,  
presenter etc.

Now how do we model this in Rails?

Thanks in advance,  
Jatinder
