Forum: Ruby on Rails Model Structure Advise

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-05-25 05:22
(Received via mailing list)
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
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-05-25 17:47
(Received via mailing list)
On 5/24/06, John Tsombakos <johnt519@gmail.com> 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
34f5b045aec62235c17458650ea75353?d=identicon&s=25 Steve Koppelman (hatless)
on 2006-05-25 18:14
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 Tsombakos 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
6edd67c92a1dab5eb23fed79f3c18564?d=identicon&s=25 David Heinemeier Hansson (Guest)
on 2006-05-25 21:13
(Received via mailing list)
> 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 Hansson
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
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-05-25 22:10
(Received via mailing list)
On 5/25/06, David Heinemeier Hansson <david.heinemeier@gmail.com> 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
F82a441e09b4725a51db48b6639ae82c?d=identicon&s=25 jatinder saundh (Guest)
on 2006-05-26 11:44
(Received via mailing list)
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
This topic is locked and can not be replied to.