This project is archived and is in readonly mode.

#5756 ✓stale
John Franklin

ActiveRecord fails to handle to_many relations sanely during

Reported by John Franklin | October 4th, 2010 @ 02:37 PM

Rails can create XML such as this:

    <foo type='float'>124.0</foo>
    <bar type='float'>0.0</bar>
    <created-at type='datetime'>2010-10-04T10:50:59Z</created-at>
    <id type='integer'>23</id>
    <updated-at type='datetime'>2010-10-04T10:50:59Z</updated-at>
    <images type='array'>
            <id type='integer'>15</id>
            <id type='integer'>16</id>
            <id type='integer'>17</id>

Feed it back into Rails, and it'll create a hash like:

{"my-ar-model"=>{"foo"=>124.0, "bar"=>0.0, "images"=>[{"id"=>15},{"id"=>16},{"id"=>17}], ...}

Yes, there are bits left out. The hash for the images to_many relation is at issue. When using this hash to create a new AR record, this dies with the following (partial) backtrace:

ActiveRecord::AssociationTypeMismatch (Image(#70247453816860) expected, got HashWithIndifferentAccess(#70247494361020)):
    /vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:256:in `raise_on_type_mismatch'
    /vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:310:in `replace'
    /vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:310:in `each'
    /vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:310:in `replace'
    /vendor/rails/activerecord/lib/active_record/associations.rb:1322:in `images='
    /vendor/rails/activerecord/lib/active_record/base.rb:2589:in `send'
    /vendor/rails/activerecord/lib/active_record/base.rb:2589:in `attributes='
    /vendor/rails/activerecord/lib/active_record/base.rb:2585:in `each'

The expected behavior is for Rails look up the Images by ID and add them to MyArModel's : images to_many relationship.

Ubuntu 9.10 (using Ubuntu's ruby1.8 and rails packages)
Ruby 1.8
Rails 2.2.3

Comments and changes to this ticket

  • Jeff Kreeftmeijer

    Jeff Kreeftmeijer October 6th, 2010 @ 04:58 PM

    • State changed from “new” to “needs-more-info”
    • Importance changed from “” to “Low”

    I think this should be marked as wontfix, but can you show us some code? That'll make it easier to understand and reproduce your problem. :)

    Putting this one on needs-more-info for now.

  • Ryan Bigg

    Ryan Bigg October 11th, 2010 @ 10:58 AM

    • Tag cleared.

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg October 19th, 2010 @ 08:25 AM

    Automatic cleanup of spam.

  • John Franklin

    John Franklin October 20th, 2010 @ 04:15 PM

    More info is coming, but obligations with hard deadlines are trumping.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:28 PM

    • State changed from “needs-more-info” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:28 PM

    • State changed from “open” to “stale”

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=""></a>