Scenario:
I have a habtm relationship and would like to determine if children in one direction have been added or deleted. I'm trying to use callbacks but find i don't have a record of changes to the children. Is there something like course.students.changed?
Using:
- Rails 3.0.3
- Ruby 1.9.2p0
Tables:
students - id, first_name, last_name
courses - id, name, location
courses_students - course_id, student_id
Models:
class Course
# Callbacks
before_save :student_maintenance
# Relationships
has_and_belongs_to_many :students
protected
def student_maintenance
# I want to do something like
students.changed?
students.changes.each do |student|
if marked_for_deletion
# do something
elsif marked_for_addition
# do something else
end
end
end
end
end
class Student
# Relationships
has_and_belongs_to_many :courses
end