#116 ✓resolved
Sam Smoot

Self-Referential HABTM

Reported by Sam Smoot | December 30th, 2007 @ 09:06 PM

Need some SR-HABTM action.

Maybe require a specific subclass of the normal HABTM class? Not sure yet...

Comments and changes to this ticket

  • asceth

    asceth January 8th, 2008 @ 03:14 AM

    I'm getting closer on this but I currently have a spec "should load self-referential associations" failing on me. It's just like the spec "should load associations" in habtm except well testing self-referential relationships.

    The problem is that the association returns itself as the entries instead of the actual related row:

    task_id, related_task_id


    So Task[1] should have Task[2] in its tasks association but instead:

    Task[1].tasks.first  # returns Task[1] instead of Task[2]

    I believe the problem is in HasAndBelongsToManyAssociation::Set#entries or in the finder but I've yet to pinpoint exactly where. Any insight into how that method works when loading the related columns would help.

    The SQL generated from Task[1].tasks.first is:

    SELECT "tasks"."id", "tasks"."name", "tasks_tasks"."task_id", "tasks_tasks"."related_task_id" FROM "tasks" JOIN "tasks_tasks" ON "tasks_tasks"."task_id" = "tasks"."id" WHERE ("tasks_tasks"."task_id" IN (1, 1))

    SELECT "id" FROM "tasks" WHERE ("id" IN (1))

    Once I get this problem down I can finish the testing of the recursive behavior where I've taken the (...., cleared) approach on #save_without_validation and #dirty?

  • asceth

    asceth January 8th, 2008 @ 07:05 PM

    • no changes were found...
  • asceth

    asceth January 8th, 2008 @ 07:05 PM

    • no changes were found...
  • asceth

    asceth January 8th, 2008 @ 07:06 PM

    • no changes were found...
  • asceth

    asceth January 8th, 2008 @ 07:06 PM

    • no changes were found...
  • asceth

    asceth January 8th, 2008 @ 07:14 PM

    habtm.diff should work against the latest trunk (rev694) but was done against rev693.

    tasks.rb should go inside /spec/models/

    *.yaml are the fixtures

    I had to attach the spec file because the diff didn't capture the changes but basically the only difference is at the bottom of that file with the new self-referential specs.

    The patch should not only make the self-referential habtm work but also allows database.save(Task) to create the habtm table. There was an error I encountered with the habtm table columns being set with NOT NULL and I fixed that as well.

    The AnimalsExhibit model in /spec/models can now be taken out. I'm going to list some code here that didn't get picked up in the diff either.


    # TODO: Get rid of the stupid AnimalsExhibit model...
        table = database.table(name.to_s)
        table.create! true

    If I think of more I'll update again. I was working on the database.save(Model) stuff before the self-referential but it helps in the spec/other cases.

  • Sam Smoot

    Sam Smoot January 13th, 2008 @ 11:37 PM

    • Assigned user changed from “Sam Smoot” to “Adam French”

    Adam, all specs currently pass for me. Remind me though... weren't you having an issue with the removed AnimalsExhibit model?

  • Sam Smoot

    Sam Smoot January 13th, 2008 @ 11:44 PM

    • State changed from “open” to “resolved”

    Ah, nevermind Adam. I remembered. Just a broken example.rb because of #fixtures. Copied the version from spec_helper and all is well now. r703. Closing this out.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket