habtm returns wrong ids for associated objects

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: 22311’s gists · GitHub 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'm figuring how to fix it, in the meanwhile I opened a ticket on LH (#1332 habtm returns wrong ids for associated objects - Ruby on Rails - rails).

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 )

Your article_categories table shouldn't have an id column for habtm
(you could also change the select to be articles.*)

Fred

Oh, you're right. I totally forgot about this.