Skip to main content

How would you nest the forum_threads in your Forum series?

General • Asked by Chris Collinsworth

I am messing around with the forum repo and I have been trying to nest the forum_threads inside of a school model. I want every school to have their own forum. What would be the best way to accomplish this? I've tried doing just the basic:

resources :schools do
resources :forum_threads do
resources :forum_post, module: :forum_threads
end
end

(that code was just written off the top of my head so forgive the inaccuracies) and the routing works, but I am struggling with how to handle the controllers.

Thanks in advance


One way you could think about handling this is with multi-tenancy,

You could set it up in a way that schools have a subdomain on your app, like school1.example.com/ and then they have forum threads nested in that like normal. and the way to differentiate between the data would be based off the subdomain or the URL.

https://rubygems.org/gems/apartment could help


That's great option. I'll look into that.

Do you have any advice on how to handle it through the routes and controllers?


Subdomains are definitely great if you want them separate. If you're building something public, you could do basically what you posted originally, although it's sometimes nice to also set the schools module as well. That will just help you organize your controllers and views nicely.

resources :schools do
  resources :forum_threads, module: :schools do
    resources :forum_post, module: :forum_threads
  end
end

And this would generate urls like /schools/my-school/forum_threads/i-have-a-question with /forum_posts tacked on the end as a route that you'll mostly just interact with through forms.

Apartment is definitely awesome if you want to separate out everything (so it's not as easy to just switch schools in the url). The way this would work is that you can use subdomain helper with Apartment. It would select the right database schema for you and keep everything separated out at the database level. You wouldn't need school_id on your forum threads or anything with that and your routes would simply be:

  resources :forum_threads, module: :schools do
    resources :forum_post, module: :forum_threads
  end

Awesome, thanks. I'll keep working through it with regular nesting and see what I can do.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.