This project is archived and is in readonly mode.

#6478 new

Error when saving nested polymorphic models in a form

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

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:

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.

Comments and changes to this ticket

  • Josep M. Bach

    Josep M. Bach February 27th, 2011 @ 02:01 PM

    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?

  • activestylus

    activestylus February 28th, 2011 @ 03:54 PM

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

  • activestylus

    activestylus February 28th, 2011 @ 09:04 PM

    • Assigned user set to “Aaron Patterson”

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>