This project is archived and is in readonly mode.
default_scope order(...), PostgreSQL, and has_and_belongs_to_many cause AssociationTypeMismatch exception
Reported by Paul Gideon Dann | August 10th, 2010 @ 03:43 PM
I've created a simple test-case to demonstrate the bug I've encountered in a larger project. I have two models: Project, and Person. They have a has_and_belongs_to_many association. The Project model has a default_scope of "order(:title)". The second time I try to modify person.projects, I get an AssociationTypeMismatch error, in the form of "Project(#XXXXX) expected, got Project(#XXXXX)".
I've traced this down to the default_scope call, and it only happens when using the PostgreSQL adaptor (with the pg gem). The attached test case illustrates the bug in its simplest form. Create a couple of projects, create a user, and try setting the user's projects a couple of times and you'll soon hit the bug.
Comments and changes to this ticket
-
Paul Gideon Dann August 11th, 2010 @ 10:06 AM
- Tag changed from activerecord-store, postgresql, rails3rc to activerecord-store, critical, postgresql, rails3rc
This seems like a really serious bug. It seems as though the PostgreSQL adaptor is generally in a bad state, really. Please look into this pretty soon; I'm worried that 3.0 will be released with Postgres as a second-class citizen :(
-
Dirk Neumann August 11th, 2010 @ 02:03 PM
- Tag changed from activerecord-store, critical, postgresql, rails3rc to activerecord-store, critical, postgresql, rails3rc, sqlite_adapter
I've encountered the same issue with sqlite3 and rails3rc
-
Paul Gideon Dann August 11th, 2010 @ 02:37 PM
- Tag changed from activerecord-store, critical, postgresql, rails3rc, sqlite_adapter to arel rails3, activerecord-store, critical, postgresql, rails3rc, sqlite_adapter
Yes, you're quite right---I can reproduce it using the testcase above with Sqlite3 as well. I'd guess that means the bug isn't related to the database adapter then. Maybe Arel? More likely something else in ActiveRecord.
-
Adam Bachman August 12th, 2010 @ 08:30 PM
+1
I thought I was going crazy. I've been seeing this for the last few weeks in the exact scenario you're describing.
Posting a form for model A with nested fields referencing model B which A associates with through habtm, using pg, and model B has a default_scope set fails with
ActiveRecord::AssociationTypeMismatch B(#1234567) expected, got B(#7654321)
.Turning off the default_scope on model B took care of the issue.
Example code at: http://gist.github.com/521545
-
Paul Gideon Dann August 31st, 2010 @ 11:15 AM
I don't understand---how can Rails 3.0 ship with this bug? We haven't even heard anything from a developer regarding this :s It would be nice to know what the plan is: will someone be able to look into this any time soon?
-
Neeraj Singh August 31st, 2010 @ 03:10 PM
- State changed from new to needs-more-info
- Importance changed from to Low
Can someone develop a test case which fails if I run with pg. You can run ActiveRecord tests with pg using command rake test_postgresql . Once we have a failing test I can look into this issue.
-
Paul Gideon Dann September 1st, 2010 @ 04:52 PM
I've had a shot at it developing that testcase, but the bug itself is clearly caused by a weird interaction involving several parts of the stack, and I can't replicate the problem on the console or in activerecord tests. Anyway, I have already provided a very simple test app that demonstrates this problem. I'm trying to track down the bug myself, but I just don't know enough about Rails internals. Someone who knows more about the whole stack would probably find a solution to this issue in a trice.
Honestly, that test app demonstrates the problem very well.
-
Neeraj Singh September 1st, 2010 @ 09:38 PM
@Paul can you try suggestion mentioned here https://rails.lighthouseapp.com/projects/8994/tickets/5497-default-...
Not sure but looks like that might help.
-
Paul Gideon Dann September 2nd, 2010 @ 10:22 AM
@Neeraj => YES! The fix committed in that ticket has also fixed this problem. The commit is present on the rails 3-0-stable branch, so I'm guessing it'll be available in Rails 3.0.1, right? When can we expect the release?
-
Neeraj Singh September 2nd, 2010 @ 11:57 AM
- State changed from needs-more-info to resolved
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>