Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8319287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:16:40+00:00 2026-06-08T22:16:40+00:00

I have 4 models. Departments , Courses , Sections , and Teacher All of

  • 0

I have 4 models. Departments, Courses, Sections, and Teacher
All of them are heirachically connected with the proper belongs_to and has_many relations. A section belongs to a course which belongs to a department. A teacher has many sections, however, a course has many (is taught by many different) teachers.

I would like to construct the site in such a way that the URL
/view/department_name/course_name/section_name/ shows the section with the name section_name, which belongs to the course named couse_name, which belongs to the department department_name.

However, I also want to /view/teacher_name/course_name/section_name to work the same way, where it shows all the sections associated with that teacher.

How can I do this?

FYI: the different models are connected by their IDs in the database. Section has a course_id integer associated with it, and course has a department_id integer. Section also has a department_id, which is redundant, but that’s how I have it set up.

my routes.rb

Test2::Application.routes.draw do
  resources :courses
  resources :departments
  resources :teachers
  resources :sections
  resources :courses
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:16:41+00:00Added an answer on June 8, 2026 at 10:16 pm

    You mentioned things like section_name in your routes. You should make sure the names are all URL-safe.

    Another point you have to consider is the two paths you want is in fact quite similar and thus you may need to handle both in the same controller action.

    /view/department_name/course_name/section_name
    /view/teacher_name/course_name/section_name
    

    The only different part is the department_name vs teacher_name. Hopefully there is no any teacher have the same name with your department XD”

    You may define the path as:

    get '/view/:department_or_teacher_name/:course_name/:section_name' => 'some_controller#some_action'
    

    Then in your action, you need something like this:

    def some_action
      department_or_teacher = Department.find_by_name(params[:department_or_teacher_name]) || Teacher.find_by_name(params[:department_or_teacher_name])
      course = Course.find_by_name(params[:course_name])
    
      q = Section.where(name: params[:section_name]).where(course_id: course.id)
      if department_or_teacher.class.name == "Department"
        q = q.where(department_id: q.id)
      elsif department_or_teacher.class.name == "Teacher"
        q = q.where(teacher_id: q.id)
      end
    
      @section = q.first # I assume there should be only 1 section with a given name, under a specific course, and (under a specific department or a specific teacher)
    end
    

    The above generates 3 queries, which might not be very good. You may consider using joins:

    def some_action
      department_or_teacher = Department.find_by_name(params[:department_or_teacher_name]) || Teacher.find_by_name(params[:department_or_teacher_name])
    
      q = Section.where(name: params[:section_name]).joins(:course).where("courses.name = ?", params[:course_name])
    
      if department_or_teacher.class.name == "Department"
        q = q.where(department_id: q.id)
      elsif department_or_teacher.class.name == "Teacher"
        q = q.where(teacher_id: q.id)
      end
    
      @section = q.first
    end
    

    There is still a difficulty in guessing the department or teacher. So if you redefine the path to something like this, the above could be cleaner:

    get '/view/department/:department_name/:course_name/:section_name' => 'some_controller#some_action_for_department'
    get '/view/teacher/:teacher_name/:course_name/:section_name' => 'some_controller#some_action_for_teacher'
    
    def some_action_for_department
      @section = Section.where(name: params[:section_name])
                        .joins(:course).where("courses.name = ?", params[:course_name])
                        .joins(:department).where("departments.name = ?", params[:department_name]) # you mentioned there is a department_id in sections, thus you could actually setup a belongs_to :department in section
                        .first
    end
    
    def some_action_for_teacher
      @section = Section.where(name: params[:section_name])
                        .joins(:course).where("courses.name = ?", params[:course_name])
                        .joins(:teacher).where("teachers.name = ?", params[:teacher_name])
                        .first
    end
    

    This is the idea. I did not test it and just by guess. So please tell me if you encountered any problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: class User include Mongoid::Document field :name, :type => String has_many
I have the following models: class Evaluation < ActiveRecord::Base attr_accessible :product_id, :description, :evaluation_institutions_attributes has_many
I have two associated models (meetings and departments). A meeting has exactly one department,
I have models like this: class Vendor(models.Model): title = models.CharField() class Product(models.Model): ... vendor
I have models Product and Category . Category can have many products. ( Product
I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model): nazwa =
I have that situation: I have models Item, Region and Country. class Item(models.Model): name
So, say I have models like this: class Foo(Model): name = CharField(max_length=200) def latest_comment(self):
I use flask with flask-peewee and wtfpeewee. So, I have models like that: class
I have two models, Listing and Invitation, associated with has_and_belongs_to_many. I am looking at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.