All threads / How would you nest the forum_threads in your Forum series?

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

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.

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.