# Nested attributes, fields\_for

**URL:** <https://www.ruby-forum.com/t/nested-attributes-fields-for/167426>\
**Category:** Rails\
**Created:** [May 7, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/nested-attributes-fields-for/167426 "2009-05-07T18:33:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [May 7, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/nested-attributes-fields-for/167426/1 "2009-05-07T18:33:01Z")

</div>

Hello,

My ignorance in Rails should be evident from my wording perhaps,  
however I couldn’t resolve the following w/o tinkering with  
action\_view/helpers/form\_builder.rb a bit. Please give me some  
pointers if possible.

I have a model using globalize2 for translations. It is actually a  
has\_many assotiation. I am updating the translated attributes through  
the nested attributes framework. I am building a form as usual and use  
fields\_for to show the translated attributes for the different locales  
to be edited. So far so good everything works as expected.

However I need to specify the order by which the different locales  
appear when fields\_for runs. For example I want the :el locale fields  
to appear before :en (yes, alphabetically, or more generally, a  
predictable ordering is need).

As it is, globalize2 doesn’t use any :order in the assosiaction  
definition and that makes sense, this path is called frequently and it  
would impose additional strain in the db backend. So the different  
locales appear with whatever order they are stored in the database (if  
that) which can be considered arbitrary.

I thought of using:

\<% f.fields\_for :globalize\_translations,  
f.object.globalize\_translations.find(:all, :order =\> “locale ASC”) do |  
t| %\> …

instead but the explicit object is ignored as it is an array and even  
then, the objects within won’t respond to new\_record method (I am  
editing an existing record).

So, any ideas? For the above to work as I’d like, I did this to  
actionpack-2.3.2/lib/action\_view/helpers/form\_builder.rb (arround line  
1039):

def fields\_for\_with\_nested\_attributes(association\_name, args, block)  
name = “#{object\_name}[#{association\_name}\_attributes]”  
association = @object.send(association\_name)

explicit\_object = args.first ###### if  
args.first.respond\_to?(:new\_record?)

if association.is\_a?(Array)

```
children = explicit_object ? [explicit_object].flatten :

```

association

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

```
explicit_child_index = args.last[:child_index] if args.last.is_a?

```

(Hash)

```
children.map do |child|
  fields_for_nested_model("#{name}[#{explicit_child_index ||

```

nested\_child\_index}]", child, args, block)  
end.join  
else  
fields\_for\_nested\_model(name, explicit\_object || association,  
args, block)  
end  
end

… which of course “works for me” but I have no idea of its side-  
effects and of course I feel less than competent about changing core  
code like this.

Please help 🙂

Thanks,  
-Kostas

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [May 11, 2009, 12:35am UTC](https://www.ruby-forum.com/t/nested-attributes-fields-for/167426/2 "2009-05-11T00:35:42Z")

</div>

Hi Kostas,

On May 7, 9:52 am, Kostas [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However I need to specify the order by which the different locales  
> appear when fields\_for runs. For example I want the :el locale fields  
> to appear before :en (yes, alphabetically, or more generally, a  
> predictable ordering is need).

[snip]

> … which of course “works for me” but I have no idea of its side-  
> effects and of course I feel less than competent about changing core  
> code like this.

I had exactly the same thoughts a couple of weeks ago and I think I  
came up with the same solution, although additionally I want to stop  
the redundant association call if we are already providing an array to  
fields\_for, as that is potentially quite expensive. A monkey patch  
version of my code (can be placed in config/initializers) is here:

> <https://gist.github.com/Odaeus/109772>

Your patch has the virtue of being a lot simpler and thus less likely  
to fail and more likely to be accepted!

Of course the workaround is in the Rails docs, simply loop over your  
association outside of field\_for, calling it for each object, but this  
seems particularly wasteful and inelegant.  
Is anyone else interested in this functionality?

All the best,  
Andrew

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [May 12, 2009, 9:17am UTC](https://www.ruby-forum.com/t/nested-attributes-fields-for/167426/3 "2009-05-12T09:17:33Z")

</div>

Hello,

Thanks for your answer. I ended up doing the workaround, as my “patch”  
was failing miserably in the general case 🙂  
Yes, you’re right, the workaround is inelegant to say the least. I’ll  
be checking up your code as well, see what you did, maybe come up with  
some idea.

Thanks again!

-K.

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [May 15, 2009, 2:30am UTC](https://www.ruby-forum.com/t/nested-attributes-fields-for/167426/4 "2009-05-15T02:30:40Z")

</div>

On May 12, 8:16 am, Kostas [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for your answer. I ended up doing the workaround, as my “patch”  
> was failing miserably in the general case 🙂  
> Yes, you’re right, the workaround is inelegant to say the least. I’ll  
> be checking up your code as well, see what you did, maybe come up with  
> some idea.

I’ve created a new ticket to allow nested attributes fields\_for to  
accept collections:  
[https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2648-accept-arrays-for-nested-attributes-fields\_for](https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2648-accept-arrays-for-nested-attributes-fields_for)  
(or [#2648 Accept arrays for nested attributes fields\_for - Ruby on Rails - rails](http://is.gd/zX7R) if that URL breaks over lines)

Please comment/+1 it if you think this would be useful.

All the best,  
Andrew
