views:

68

answers:

2

Consider a typical social networking website, which has more or less the following models:

User 
Blog, Posts
Forums, Topics, Responses
Wiki, Pages
....
....

I want to introduce a model called Site/Space where each User can have one or many sites/spaces. And I want to provide a way for the Site owner to select many features (or call it apps/tools).

Whats the best way to design this model - so called feature/app/tool?

Note: In many cases each feature may not be the same as corresponding model. Lets consider blog feature, by enabling the Blog feature, I should be able to associate (some how) that the corresponding site has access to both Blog & Post, (another example) by enabling Forums feature, I should be able to associate the site has access to not only Forum, but also Topic & Response models. I need these checks so that I could define a before_filter to check if a particular site has access to the content or not.

I looked at some open source rails applications that have this kind of on demand features, but by looking at the form attributes, it looks like they have has_blog, has_post,... fields tucked in the Sites table, in my situation it may not work as the number of these models may grow. Do you still think that adding these boolean fields in the Sites table is the best approach?

A: 

Define a many-to-many relationship between an Apps and a Sites table in your database.

Apps

  • AppID
  • Name
  • ...

Sites

  • SiteID
  • UserID
  • ...

SiteApps

  • SiteID
  • AppID

    SELECT AppID FROM SiteApps WHERE SiteID=...

See also SQL: Many-To-Many table AND query

Thorarin
But how would I know by adding Blog app, that the site has access to both blog model as well as post model?
satynos
A: 

polymorphic associations, has_many :through, and/or has_and_belongs_to_many sound like the building blocks to your solution. I'd look into them.

Ben Hughes
But by adding Blog app, how would I be able to know that the site has access to both blog and post models?
satynos
polymorphic association. It is not clear what you are trying to do, but for handling an association (singular or plural) that can reference items of multiple types, they are generally the right way to go. I'd also examine your system to see if you can simplify any of the relationships.
Ben Hughes