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 269211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:48:03+00:00 2026-05-11T23:48:03+00:00

I have two scaffold-generated Models, student and class . They have a many-to-many relationship

  • 0

I have two scaffold-generated Models, student and class. They have a many-to-many relationship implemented with has_and_belongs_to_many.
I’d like to be able to change which classes a student is in as well as which students are taking each class. That is, I want to modify a student‘s classes variable (adding and removing items from it) and vice versa.
How do I do this RESTfully?
If I’m removing a class from a student‘s classes list, then it seems like I want to call update on my students_controller. If this is the case, then what should I pass in as a parameter to modify the classes variable? Another collection of classes (with the proper class removed)?
My other thought is just to call some action in the students_controller (say, remove_class) and pass in the ID of the class to be removed. This seems sensical but not RESTful.

What’s the best way to do this?

  • 1 1 Answer
  • 1 View
  • 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-05-11T23:48:04+00:00Added an answer on May 11, 2026 at 11:48 pm

    The key to resolving this is to correctly identify the resource you are modifying. In this case, the resource you are modifying is the relationship between the class and the student, which I will refer to as an Enrollment.

    It has become customary in Rails to use has_many :through preferentially to has_and_belongs_to_many. You may want to change your domain logic to fit the custom, but you can also buck the trend, if you are truly certain that no metadata needs to be stored about the relationship.

    One of the key ideas for REST is that RESTful resources do not need to map to models. You should create an EnrollmentsController and add a line to config/routes.rb:

    map.resources :enrollments
    

    Then you can create and delete your relationships like so:

    class EnrollmentsController < ApplicationController
        def create
           @student = Student.find(params[:student_id])
           @course = Course.find(params[:course_id])
           @student.courses << @course
           if @student.save
             #do happy path stuff
           else
             #show errors
           end
        end
    
        def destroy
           @student = Student.find(params[:student_id])
           @course = @student.courses.find(params[:course_id])
           @student.courses.delete( @course )
        end
    end
    

    And you can make buttons for those actions like so:

    <%= button_to "Enroll", enrollments_path(:student_id => current_student.id, :course_id => @course.id ), :method => :post %>
    <%= button_to "Withdraw", enrollment_path(1, :student_id => current_student.id, :course_id => @course.id ), :method => :delete %>
    

    The 1 on the line above acts as a placeholder where the :enrollment_id should go and is a tiny bit of syntactic vinegar to remind you that you are bucking the will of the Rails framework.

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

Sidebar

Related Questions

I have two tables here both with their respective scaffold generated controllers and models:
I have two classes generated by LINQ2SQL both from the same table so they
I have two classes Order and Items I want a method like this class
have two three tables one for student, second for class, and the third for
I have three models : class LevelTwoArea < ActiveRecord::Base has_many :places, dependent: :restrict end
Have two bean definitions: file a.xml <bean id=A class=com.A> <property name=bClass ref=B/> </bean> file
I have two convex polygons in 3D. They are both flat on different planes,
I have a basic scaffold for products. I would like to have a unique
Have two folders with approx. 150 java property files. In a shell script, how
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two

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.