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.

Error when saving nested polymorphic models in a form

#6478

In the case of Person has_many :phones, :as => :phoneable, this happens when I submit a nested form.

Phone(#2157249660) expected, got Array(#2151973780)

You can test it for yourself with this app: https://github.com/activestylus/nested_polymorphic_attributes_bug

On closer inspection it appears the form helpers are not rendering the nested fields correctly, particularly the name attribute:

Looking at the source I see:

<input name="person[phones][number]"...

When I'm pretty sure that should be:

<input name ="person[phones][0][number]"...

FWIW I can build nested models in the console with no problems whatsoever. And the form helpers do not suffer this problem with a regular has_many relationship.

Reported by activestylus · February 27th, 2011 @ 12:12 PM

State: new
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: none

Activity

  1. Josep M. Bach
    Josep M. Bach

    Seems that the builder doesn't know it's a one-to-many relationship or something like that, could it be?

    In this particular line it checks for the [] naming after person[phones] - if it had found it, it would have triggered the #retrieve_auto_index method, which assigns an index ([0], [1]...). Sadly it matches nothing, so I'm guessing someone in the call stack is not checking if person->phones is a 1-n relationship.

    Any ideas?

    February 27th, 2011 @ 02:01 PM

  2. activestylus
    activestylus

    Just tested in 2.3.8 and works fine. Appears to be a Rails 3 issue.

    February 28th, 2011 @ 03:54 PM

  3. activestylus
    activestylus
    • Assigned user set to Aaron Patterson

    February 28th, 2011 @ 09:04 PM