Forum: Ruby on Rails Self-referential join creation/deletion and :through

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5afd6080ad6ed43eb305e9e9a0309a74?d=identicon&s=25 David Willis (dawillis)
on 2006-04-05 19:19
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?
5afd6080ad6ed43eb305e9e9a0309a74?d=identicon&s=25 David Willis (dawillis)
on 2006-04-06 00:02
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?
6f7c877de704c7cc03c8a3b2dc52df92?d=identicon&s=25 Carmen --- (carmen)
on 2006-04-06 04:14
David Willis 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
6f7c877de704c7cc03c8a3b2dc52df92?d=identicon&s=25 Carmen --- (carmen)
on 2006-04-06 04:19
carmen  wrote:
> David Willis 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 :)
5afd6080ad6ed43eb305e9e9a0309a74?d=identicon&s=25 David Willis (dawillis)
on 2006-04-06 06:07
carmen  wrote:
> carmen  wrote:
>> David Willis 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.
This topic is locked and can not be replied to.