Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord fails to handle to_many relations sanely during MyArModel.new()

#5756

Rails can create XML such as this:

<my-ar-model>
    <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'>
        <image>
            <id type='integer'>15</id>
        </image>
        <image>
            <id type='integer'>16</id>
        </image>
        <image>
            <id type='integer'>17</id>
        </image>
    </images>
</my-ar-model>

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.

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

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

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • 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.

    October 6th, 2010 @ 04:58 PM

  2. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:58 AM

  3. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:25 AM

  4. John Franklin
    John Franklin

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

    October 20th, 2010 @ 04:15 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:28 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:28 PM