Model Structure Advise

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

On 5/24/06, John T. [email protected] 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

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

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 – Broadcasting Brain
http://www.basecamphq.com – Online project management
http://www.backpackit.com – Personal information manager
http://www.rubyonrails.com – Web-application framework

On 5/25/06, David Heinemeier H. [email protected] 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” :slight_smile:

jt

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