This project is archived and is in readonly mode.
ActiveRecord::AssociationTypeMismatch with same class name -- added helpful exception message
-
Arya Asemanfar
I'm not sure if this is required or not, but here is my commit on my fork of Rails on GitHub:
-
Arya Asemanfar
Sorry about that. I didn't know I wasn't supposed to fork Rails unless it's some big change. I deleted my fork.
-
Pratik
- State changed from new to invalid
I think this should be in a plugin like http://groups.google.com/group/r...
Thanks!
-
Arya Asemanfar
Hi Pratik,
If the suggestion-type language is too superfluous for a Rails error message, may I suggest two alternatives that avoids the Windows-esque type error message of "User expected, got User":
1. What do you think about adjusting changing the error message to something like "older version of User expected, received a newer version of User" if the two class names are textually identical? I'm not sure about the word 'version' in that sentence, but something to indicate to the developer that they simply received the error because there are 2 definitions of the class, and we were expecting an older one.
2. This one is a little more active in solving the problem, although I'm not sure if it's a valid solution. What if instead of comparing @reflection.klass and record.class, it compared the string representations of them? So it wouldn't raise an exception when it receives User and expected and older version of User. Are there any cases where it's unsafe to assume that it's the same class but just an older version?
Anyway, please let me know what you think. I'm just trying to help some people save time considering the "User expected, got User" error message is unclear.
-
Jeremy McAnally
I'm not sure why it's just showing the class name rather than something like "expected User (), got User (#343 assume_migrated_upto_version doesn't work with non-standard migrations path e4>)." It'd be trivial to add a call to #inspect and would fix the biggest problem with this error message.
Ideally it'd be nice to show the class's ancestors or something too but maybe that's too verbose.
-
Star Trader
+1
This is a needed clarification of an otherwise nonsense error message. It installs properly and works as advertised. I found no bugs or performance issues, and all tests still pass.
Active record's extensive framework make hand debugging difficult. I believe that it has been, and should continue to be the policy of the maintainers to provide clear and helpful error messages by default. I respectfully disagree with Pratik that this type od developer assistance should not come with the default package.
I request that this ticket be reopened and that once sufficient testing has been don that it be merged into Edge Rail. While I find no reason not to also include it in the 2.1 release, it is non-critical.
-
Rick
- State changed from invalid to open
I don't like the error message itself. I'd personally go with Jeremy's suggestion of showing the object_ids of the two differing classes, with a note in the has_many docs explaining why this happens.
Also, you should look into making your plugins reloadable. Though, that should really be documented so folks know the option is available (and honestly has been since Rails 1.1.x).
-
Roger
http://dev.rubyonrails.org/ticke... seems to have a nice patch.
-
Repository
- State changed from open to resolved
(from [19d7b1d22bd68af00244ddc3e1f35cec187e9120]) Verbose ActiveRecord::AssociationTypeMismatch exception message. [#189 state:resolved]
-
Lawrence Pit
- Tag set to activerecord, enhancement
See #229 raise_on_type_mismatch : expected User, got User if you still experience issues with this.
-
Ryan Bigg
Automatic cleanup of spam.
