This project is archived and is in readonly mode.

#5795 ✓resolved
Nigel Ramsay

fields_for ignores supplied collection (ActiveRecord::Relation)

Reported by Nigel Ramsay | October 12th, 2010 @ 11:26 PM | in 3.0.2

When supplying a collection for use with the fields_for helper method, it will only be used if the object is an Array. It should also work with an ActiveRecord::Relation.

Example

See the example, taken from http://railsapi.com/doc/rails-v3.0.0/classes/ActionView/Helpers/For...)

edit.html.erb

<%= form_for @person do |person_form| %>
    ...
    <%= person_form.fields_for :projects, @active_projects do |project_fields| %>
      Name: <%= project_fields.text_field :name %>
    <% end %>
<% end %>

The @active_projects variable is only used when it is forced into an Array:

people_controller.rb

  def edit
    ...
    # when using this scenario, the @active_projects collection is not used
    @active_projects = @person.projects.where(:active => true)

    # when forcing the conversion to an array, @active_projects is used
    @active_projects = @person.projects.where(:active => true).all
  end

Note: when the @active_projects variable is not used, the default @person.projects association is used.

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Tags