I have Classroom model Like:
class Classroom < ApplicationRecord
has_many :students,dependent: :destroy
end
I have Student model Like:
class Student < ApplicationRecord
belongs_to :classroom, optional: true
validate :check_student_limit
def check_student_limit
if Student.where(classroom_id: self.classroom_id).count > 4
self.errors.add(:name, "Over limit of student in classroom")
end
end
end
Classroom has_many
student so
I want to update the selected students classroom.
Suppose I have a classroom with id: 4. Now I select the three students of that classroom which has an id (14,15,16) and I also select the id in which class I want to move these three students. Let's say I want to move these three students in Classroom_id: 5, These three students should move into the classroom 5, and three students delete from classroom 4.
I create a new form for selecting passed students and a new classroom for those students.
In my action controller for this form
I get the selected students_ids
and new classroom id
But now I'm stuck here. How can I insert all these selected students into the selected classroom? I'm troubling here to solve this, please help me if you can.
Here the Images for the reference.
This is how can I selected the students and new classrooms:
I got the following params in the console: