Greetings.
First, this example is just my way of exploring :through. It probably
doesn’t need has_many :through, and could just use a standard HABTM
association.
Here’s the models:
class CourseRequisite < ActiveRecord::Base
belongs_to :requisite, :class_name => ‘Course’, :foreign_key =>
‘requisite_id’
belongs_to :course, :class_name => ‘Course’, :foreign_key =>
‘corse_id’
end
class Course < ActiveRecord::Base
has_many :requisite_courses, :class_name => ‘CourseRequisite’,
:foreign_key => ‘course_id’
has_many :requisites, :through => :requisite_courses, :source =>
:requisite
has_many :courses_requiring, :class_name => ‘CourseRequisite’,
:foreign_key => ‘requisite_id’
has_many :requisite_for, :through => :courses_requiring, :source =>
:course
end
Now, assuming that records for two courses already have been created,
and we simply want to create the “requisite” relationship between the
two records, this seems to work properly:
course = Course.find_by_name(“Econ 102”)
requisite = Course.find_by_name(“Econ 101”)
course.requisite_courses.create( :requisite => requisite )
Here’s the issue I’m having: How to you delete the relationship? I’ve
tried the following which does not seem to work:
course.requisite_courses.delete(requisite)
It fails because it wants to find the record for requisite using an ‘id’
column, but since it’s a record in a join table, there isn’t an ‘id’
column, just the course_id, requisite_id, and the attribute columns.
Am I not seeing the complete picture?