Ask A Question

Notifications

You’re not receiving notifications from this thread.

How would you nest the forum_threads in your Forum series?

Chris Collinsworth asked in General

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

Reply

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

Reply

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

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

Reply

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
Reply

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

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.