Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

habtm returns wrong ids for associated objects

#1332

I have a vanilla habtm association between Article and Category.


Article.all
# => [#<Article id: 1, title: "Rails Conf 2009", created_at: "2008-11-05 10:28:10", updated_at: "2008-11-05 10:28:10">, #<Article id: 2, title: "Cached Models", created_at: "2008-11-05 10:28:10", updated_at: "2008-11-05 10:28:10">]

Now we try to load the same articles via habtm association:


Category.first.articles
# => [#<Article id: 1, title: "Cached Models", created_at: "2008-11-05 10:28:10", updated_at: "2008-11-05 10:28:10">, #<Article id: 2, title: "Rails Conf 2009", created_at: "2008-11-05 10:28:10", updated_at: "2008-11-05 10:28:10">]

Look at the article ids in the first and the second case.

Here the query used by habtm:


SELECT * FROM "articles" INNER JOIN "articles_categories" ON "articles".id = "articles_categories".article_id WHERE ("articles_categories".category_id = 1 )

With sqlite3 It returns: http://gist.github.com/22311 SQLiteAdapter#select exclude all the extra and unknown columns ad returns a wrong record id for the current row.

This problem also affects MySqlAdapter

I'll try to write a patch for this.

Reported by Luca Guidi · November 5th, 2008 @ 11:11 AM

State: invalid
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Luca Guidi
    Luca Guidi

    It seems everything is ok. In the above example I forgot to set :id => false on join table migration.

    November 5th, 2008 @ 02:27 PM

  2. Frederick Cheung
    Frederick Cheung
    • State changed from new to invalid

    Seems you found your problem :-)

    December 8th, 2008 @ 03:07 PM