This project is archived and is in readonly mode.
habtm returns wrong ids for associated objects
Reported by Luca Guidi | November 5th, 2008 @ 11:11 AM | in 2.x
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.
Comments and changes to this ticket
-
Luca Guidi November 5th, 2008 @ 02:27 PM
It seems everything is ok. In the above example I forgot to set :id => false on join table migration.
-
Frederick Cheung December 8th, 2008 @ 03:07 PM
- State changed from new to invalid
Seems you found your problem :-)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>