# Fields\_for inserts 2 identical rows to the connecting table (has\_many :through)

**URL:** <https://www.ruby-forum.com/t/fields-for-inserts-2-identical-rows-to-the-connecting-table-has-many-through/188051>\
**Category:** Rails\
**Created:** [May 5, 2010, 12:26am UTC](https://www.ruby-forum.com/t/fields-for-inserts-2-identical-rows-to-the-connecting-table-has-many-through/188051 "2010-05-05T00:26:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oreng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e8cbc/32.png) [@oreng](https://www.ruby-forum.com/u/oreng)\
**Post date:** [May 5, 2010, 12:26am UTC](https://www.ruby-forum.com/t/fields-for-inserts-2-identical-rows-to-the-connecting-table-has-many-through/188051/1 "2010-05-05T00:26:07Z")

</div>

My models are TranslationKey, TranslationValue and KeyValue.

Here is the relevant part in the log: (the last two lines are  
identical inserts!)

Processing TranslationKeysController#create (for 127.0.0.1 at  
2010-05-04 22:13:31) [POST]  
Parameters: {“commit”=\>“Create”, “authenticity\_token”=\>“J+dt/  
qHwHkbKygh0wBPnXPLtpkb3Pb8URxGucEbpLa0=”,  
“translation\_key”=\>{“name”=\>“key1”,  
“translation\_values\_attributes”=\>{“0”=\>{“text”=\>“value1”}}}}

INSERT INTO `translation_keys` (`name`, `created_at`, `updated_at`)  
VALUES(‘key1’, ‘2010-05-04 22:13:31’, ‘2010-05-04 22:13:31’)  
INSERT INTO `translation_values` (`created_at`, `updated_at`, `text`,  
`language_id`) VALUES(‘2010-05-04 22:13:31’, ‘2010-05-04 22:13:31’,  
‘value1’, NULL)  
INSERT INTO `key_values` (`translation_key_id`,  
`translation_value_id`, `created_at`, `updated_at`) VALUES(16, 27,  
‘2010-05-04 22:13:31’, ‘2010-05-04 22:13:31’)  
INSERT INTO `key_values` (`translation_key_id`,  
`translation_value_id`, `created_at`, `updated_at`) VALUES(16, 27,  
‘2010-05-04 22:13:31’, ‘2010-05-04 22:13:31’)

And here is the new template:

\<% form\_for(@key) do |f| %\>  
\<% if f.error\_messages.length \> 0 %\>

  
\<%= error\_messages\_for :translation\_key, :header\_message =\>  
‘Error’, :message =\> ‘’ %\>  

  
\<% end %\> 

\<%= f.label :name, 'Key name' %\>   
 \<%= f.text\_field :name %\>

\<% f.fields\_for :translation\_values do |value\_fields| %\> \<%= value\_fields.label :text, 'Content' %\>   
 \<%= value\_fields.text\_area :text %\> \<% end %\>

\<%= f.submit 'Create' %\>

\<% end %\>

---

<div class="post-metadata">

**Author:** ![oreng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e8cbc/32.png) [@oreng](https://www.ruby-forum.com/u/oreng)\
**Post date:** [May 5, 2010, 12:43am UTC](https://www.ruby-forum.com/t/fields-for-inserts-2-identical-rows-to-the-connecting-table-has-many-through/188051/2 "2010-05-05T00:43:06Z")

</div>

btw, my new action has this:

@key = TranslationKey.new  
@key.translation\_values.build
