This project is archived and is in readonly mode.
fields_for ignores supplied collection (ActiveRecord::Relation)
-
Nigel Ramsay
- Tag changed from fields_for, form_helper, relation to 3.0.0, fields_for, form_helper, relation
-
Nigel Ramsay
This problem originates in form_helper.rb:1159:
case record_or_name_or_array when String, Symbol if nested_attributes_association?(record_or_name_or_array) return fields_for_with_nested_attributes(record_or_name_or_array, args, block) else name = "#{object_name}#{index}[#{record_or_name_or_array}]" end when Array # 1159 object = record_or_name_or_array.last name = "#{object_name}#{index}[#{ActiveModel::Naming.singular(object)}]" args.unshift(object) else object = record_or_name_or_array name = "#{object_name}#{index}[#{ActiveModel::Naming.singular(object)}]" args.unshift(object) endrather than checking for an Array class, we should really be checking for an "array-like" class.
-
Nigel Ramsay
Perhaps the replacement check should be similar to that used in partials.rb:302
if @object.respond_to?(:to_ary) -
Ryan Bigg
- State changed from new to incomplete
- Importance changed from to Low
Please submit a failing test case for this issue as per this guide: http://guides.rubyonrails.org/contributing_to_rails.html
-
Neeraj Singh
- State changed from incomplete to open
- Milestone set to 3.0.2
- Assigned user changed from Michael Koziarski to Neeraj Singh
Code fix is here. I tested it and it works. However writing writing the test is taking some time. Hopefully will finish the test tomorrow. I will submit a proper patch when test is ready. In the meantime here is the code change.
def fields_for_with_nested_attributes(association_name, args, block) name = "#{object_name}[#{association_name}_attributes]" options = args.extract_options! association = args.shift association = convert_to_model(association) debugger if association.respond_to?(:persisted?) association = [association] if @object.send(association_name).is_a?(Array) #elsif !association.is_a?(Array) elsif !association.respond_to?(:to_ary) association = @object.send(association_name) end #if association.is_a?(Array) if association.respond_to?(:to_ary) explicit_child_index = options[:child_index] output = ActiveSupport::SafeBuffer.new association.each do |child| output << fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, options, block) end output elsif association fields_for_nested_model(name, association, options, block) end end -
Neeraj Singh
- Assigned user changed from Neeraj Singh to José Valim
- Tag set to patched
Attached is code patch with test.
-
Repository
- State changed from open to resolved
(from [86f66e9097e177727627ea19e52e027cb0293f82]) fields_for should treat ActiveRecord::Relation as an array
[#5795 fields_for ignores supplied collection (ActiveRecord::Relation) state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/86f66e9097e177727627ea19e52e0... -
Repository
(from [fbaf3a23d909bca79bae3ebb77249483fcab5383]) fields_for should treat ActiveRecord::Relation as an array
[#5795 fields_for ignores supplied collection (ActiveRecord::Relation) state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/fbaf3a23d909bca79bae3ebb77249...
