Hi,
I've got these tables:
**Sites**
:has_many :blogs
:has_many :pages
**Blogs**
:belongs_to :site
**Pages**
:belongs_to :site
:belongs_to :blog
Basically, I want to be able to create Pages that are either related to a Site OR related to a Blog with routes like this:
/blogs/1/pages/1
/sites/1/pages/2
With my current setup, my Pages table has a foreign_key for blog_id AND site_id - and I was just thinking of doing this:
if a page is being created for a site (meaning it doesn't belong to a blog) then set blog_id = to NULL, but set site_id accordingly
but, if a page is being created for a blog (which already belongs to a site) then set the related site_id AND blog_id
Then if I want a list of Site pages: I can just query the Pages table for all NULL blog_ids, and if I want Blog pages, I'll get them through the relationship with Blog already.
UPDATE: I accepted the answer below which suggested using "polymorphic associations", but could this also be done using Single Table Inheritance? If so, which method is better?
Thanks.