Save 36% for Black Friday! Learn more

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I remove associated records?

azal-io asked in Rails

I have these associations.

Course
has_many :assignments, class_name: 'CourseManagement::Assignment'
has_many :students, through: :assignments, source: :student

Assignment:
belongs_to :course, class_name: 'CourseManagement::Course'
belongs_to :student, class_name: 'CourseManagement::Student'

Attendance
belongs_to :student, class_name: 'CourseManagement::Student'
belongs_to :session, class_name: 'CourseManagement::Session'

Student:
has_many :assignments, class_name: 'CourseManagement::Assignment'
has_many :courses, through: :assignments, source: :course

has_many :attendances, class_name: 'CourseManagement::Attendance'
has_many :sessions, through: :attendances, source: :session

Session:
belongs_to :course, class_name: 'CourseManagement::Course'

has_many :attendances, class_name: 'CourseManagement::Attendance'
has_many :students, through: :attendances, source: :student

What I want to do.

I have a feature that on Course form, users can select which students are taking the course.
Then when we access that course's session we can select which of these students attended to that session. The issue is when I remove student from the course, I also want that action remove all associated sessions with that student on that session.

So when again we add the student to course we don't see the student on that session.

Thanks

Reply

have you tried this? or some variation of dependent: :destroy on the model associations

has_many :students, through: :attendances, source: :student, dependent: :destroy

https://guides.rubyonrails.org/association_basics.html

Reply
Join the discussion
Create an account Log in

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

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

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