# Self-referential join creation/deletion and :through

**URL:** <https://www.ruby-forum.com/t/self-referential-join-creation-deletion-and-through/56912>\
**Category:** Rails\
**Created:** [April 5, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/self-referential-join-creation-deletion-and-through/56912 "2006-04-05T19:19:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 5, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/self-referential-join-creation-deletion-and-through/56912/1 "2006-04-05T19:19:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 6, 2006, 12:02am UTC](https://www.ruby-forum.com/t/self-referential-join-creation-deletion-and-through/56912/2 "2006-04-06T00:02:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 6, 2006, 4:14am UTC](https://www.ruby-forum.com/t/self-referential-join-creation-deletion-and-through/56912/3 "2006-04-06T04:14:29Z")

</div>

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](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 6, 2006, 6:07am UTC](https://www.ruby-forum.com/t/self-referential-join-creation-deletion-and-through/56912/4 "2006-04-06T06:07:36Z")

</div>

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 🙂

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.

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [April 6, 2006, 4:19am UTC](https://www.ruby-forum.com/t/self-referential-join-creation-deletion-and-through/56912/5 "2006-04-06T04:19:42Z")

</div>

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 🙂
