This project is archived and is in readonly mode.

#2648 ✓resolved
Andrew France

Accept arrays for nested attributes fields_for

Reported by Andrew France | May 15th, 2009 @ 01:20 AM | in 2.3.6

It would be nice to supply an array to fields_for when working with nested attributes, like so:

# In controller:
@selected_books = @author.books.all(:conditions => {:selected => true})


form_for :author do |f| f.text_field :name f.fields_for(:books, @selected_books) do |f2|
f2.text_field :name

end end

This is instead of iterating over the array as currently shown in the docs:

form_for :author do |f|
  @selected_books.each do |book|

f.fields_for(:books, book) do |f2|

end end

Supplying an array seems more elegant to me and stops the parent object association being called for every iteration.

The supplied patch passes the existing tests and adds a couple more that are appropriate. Although the nested if statements are a bit messy so any help making it cleaner would be welcome.

Comments and changes to this ticket

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>


Referenced by