Self-referential join creation/deletion and :through

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?

So there’s nobody out there who can answer this question? It’s all well
and good that HABTM with attributes are deprecated, but are there any
instructions on how to use the replacement?

David W. wrote:

So there’s nobody out there who can answer this question? It’s all well
and good that HABTM with attributes are deprecated, but are there any
instructions on how to use the replacement?

check out http://blog.hasmanythrough.com

carmen wrote:

carmen wrote:

David W. wrote:

So there’s nobody out there who can answer this question? It’s all well
and good that HABTM with attributes are deprecated, but are there any
instructions on how to use the replacement?

and the wiki entries, particularly the ones discussing the use of
:through and :polymorphic as decorated linkage classes…

i mean… :requisite_courses, :requisites, :courses_requiring,
:requisite_for …im getting dizzy :slight_smile:

Either I must be blind or dense, or your suggestions have no merit. The
only example of actually creating using the join model I’ve been able to
find in the wiki, various blogs, and such is in the Pursuing Beauty
slides DHH produced. I still have yet to see an example of the deletion
(heck, even a retrieval of) a join model record other than just grabbing
the first on the list via model.join_model.first method.

I still can’t see what it is I’m supposed to be seeing. I’ve even taken
a step back and played with DHH’s Authors, Books, Authorships example
from the Pursuing Beauty slides and I still can’t figure out how to
cleanly remove the join with attributes entry, other than
course.requisite_courses.find(:first, :conditions => [“requisite_id =
?”, requisite.id]).destroy. It just doesn’t feel right, though.

Are join models using has_many :through intended to be create only? It
just feels like the RUD part of the CRUD functionality is missing.

carmen wrote:

David W. wrote:

So there’s nobody out there who can answer this question? It’s all well
and good that HABTM with attributes are deprecated, but are there any
instructions on how to use the replacement?

and the wiki entries, particularly the ones discussing the use of
:through and :polymorphic as decorated linkage classes…

i mean… :requisite_courses, :requisites, :courses_requiring,
:requisite_for …im getting dizzy :slight_smile: