This project is archived and is in readonly mode.

#189 ✓resolved
Arya Asemanfar

ActiveRecord::AssociationTypeMismatch with same class name -- added helpful exception message

Reported by Arya Asemanfar | May 14th, 2008 @ 12:10 AM

ActiveRecord raises an exception when the expected class for an association differs from the class of object trying to be assigned.

Example:

@post.comments << "This is a string"

This works as expected and says raises an exception with "Comment expected, got String".

But sometimes, this can happen:

@comment.user = current_user

and an "User expected, got User" exception is raised. Which at first glance doesn't make sense. What happened here is the Comment model is in a plug-in and since the Comment model already got loaded and isn't re-loaded every request in development (since it's in a plugin), the User class the Comment association is expecting differs from the User class that is in the current runtime.

Anyway, I wrote a patch to inform the user that the class was changed and that they should try restarting mongrel.

Patch attached.

Comments and changes to this ticket

  • Arya Asemanfar

    Arya Asemanfar May 14th, 2008 @ 12:12 AM

    I'm not sure if this is required or not, but here is my commit on my fork of Rails on GitHub:

    http://github.com/arya/rails/com...

  • Arya Asemanfar

    Arya Asemanfar May 14th, 2008 @ 02:13 AM

    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

    Pratik May 14th, 2008 @ 08:55 AM

    • State changed from “new” to “invalid”

    I think this should be in a plugin like http://groups.google.com/group/r...

    Thanks!

  • Arya Asemanfar

    Arya Asemanfar May 14th, 2008 @ 04:30 PM

    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

    Jeremy McAnally May 14th, 2008 @ 09:35 PM

    I'm not sure why it's just showing the class name rather than something like "expected User (), got User (#343e4>)." 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

    Star Trader May 14th, 2008 @ 10:29 PM

    +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

    Rick May 15th, 2008 @ 07:23 PM

    • 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
  • Repository

    Repository May 21st, 2008 @ 10:50 AM

    • State changed from “open” to “resolved”

    (from [19d7b1d22bd68af00244ddc3e1f35cec187e9120]) Verbose ActiveRecord::AssociationTypeMismatch exception message. [#189 state:resolved]

    http://github.com/rails/rails/co...

  • Lawrence Pit

    Lawrence Pit October 2nd, 2008 @ 04:58 AM

    • Tag set to activerecord, enhancement

    See #229 if you still experience issues with this.

  • Ryan Bigg

    Ryan Bigg October 9th, 2010 @ 10:12 PM

    • Tag cleared.

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg October 21st, 2010 @ 03:37 AM

    Automatic cleanup of spam.

  • bingbing

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>

Attachments

Referenced by

Pages