# Duplicate inserts with field\_for and has\_many :through

**URL:** <https://www.ruby-forum.com/t/duplicate-inserts-with-field-for-and-has-many-through/187993>\
**Category:** Rails\
**Created:** [May 3, 2010, 9:53pm UTC](https://www.ruby-forum.com/t/duplicate-inserts-with-field-for-and-has-many-through/187993 "2010-05-03T21:53:29Z")\
**Posts on this page:** 1\
**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 3, 2010, 9:53pm UTC](https://www.ruby-forum.com/t/duplicate-inserts-with-field-for-and-has-many-through/187993/1 "2010-05-03T21:53:29Z")

</div>

I see 2 inserts of KeyValue rows on TranslationValue#create action  
(and I should only have one). it inserts identical values.

Here are my models, the edit template, the create action and the log:

models:

class TranslationKey \< ActiveRecord::Base  
has\_many :key\_values  
has\_many :translation\_values, :through =\> :key\_values  
end

class TranslationValue \< ActiveRecord::Base  
has\_many :key\_values  
has\_many :translation\_keys, :through =\> :key\_values  
accepts\_nested\_attributes\_for :translation\_keys  
end

class KeyValue \< ActiveRecord::Base  
belongs\_to :translation\_value  
belongs\_to :translation\_key  
end

edit tepmlate:

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

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

  
\<%  
end  
%\>

\<% f.fields\_for :translation\_keys do |key\_form| %\> \<%= key\_form.label :name, 'Key name' %\>   
 \<%= key\_form.text\_field :name %\>   
  
 \<%= key\_form.label :tag\_list, 'Add Tags' %\> (separate tags with a comma ',' )   
 \<%= key\_form.text\_field :tag\_list %\> \<% end %\>

\<%= f.label :text, 'Content' %\>  
 \<%= f.text\_area :text %\>

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

\<% end %\>

create action:

def create  
@content = TranslationValue.new

```
respond_to do |format|
  if @content.update_attributes( params[:translation_value] )
    flash[:notice] = 'Key was successfully created.'
    format.html { redirect_to(translation_keys_path) }
    format.xml { render :xml => @content, :status

```

=\> :created, :location =\> @content }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @content.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

Processing TranslationValuesController#create (for 127.0.0.1 at  
2010-05-03 19:47:03) [POST]  
Parameters: {“commit”=\>“Create”,  
“translation\_value”=\>{“text”=\>“value13”,  
“translation\_keys\_attributes”=\>{“0”=\>{“name”=\>“key13”,  
“tag\_list”=\>“”}}}, “authenticity\_token”=\>“J+dt/  
qHwHkbKygh0wBPnXPLtpkb3Pb8URxGucEbpLa0=”}

Language Load (0.1ms) SELECT \* FROM `languages`  
TranslationValue Columns (0.6ms) SHOW FIELDS FROM  
`translation_values`  
TranslationKey Columns (0.5ms) SHOW FIELDS FROM `translation_keys`  
SQL (0.0ms) BEGIN  
TranslationKey Load (0.2ms) SELECT `translation_keys`.id FROM  
`translation_keys` WHERE (`translation_keys`.`name` = BINARY ‘key13’)  
LIMIT 1  
CACHE (0.0ms) SELECT `translation_keys`.id FROM `translation_keys`  
WHERE (`translation_keys`.`name` = BINARY ‘key13’) LIMIT 1  
TranslationValue Create (0.1ms) INSERT INTO `translation_values`  
(`created_at`, `updated_at`, `text`, `language_id`) VALUES(‘2010-05-03  
19:47:03’, ‘2010-05-03 19:47:03’, ‘value13’, NULL)  
TranslationKey Create (0.1ms) INSERT INTO `translation_keys`  
(`name`, `created_at`, `updated_at`) VALUES(‘key13’, ‘2010-05-03  
19:47:03’, ‘2010-05-03 19:47:03’)  
Tag Load (0.2ms) SELECT `tags`.\* FROM `tags` INNER JOIN `taggings`  
ON `tags`.id = `taggings`.tag\_id WHERE (((taggings.context = ‘tags’  
AND taggings.tagger\_id IS NULL) AND ((`taggings`.taggable\_id = 8) AND  
(`taggings`.taggable\_type = ‘TranslationKey’))) AND  
((`taggings`.taggable\_id = 8) AND (`taggings`.taggable\_type =  
‘TranslationKey’)))  
Tagging Load (0.2ms) SELECT \* FROM `taggings` WHERE  
(((`taggings`.`tagger_id` IS NULL AND `taggings`.`tag_id` IN (NULL)  
AND `taggings`.`context` = ‘tags’ AND `taggings`.`tagger_type` IS  
NULL) AND (`taggings`.taggable\_id = 8 AND `taggings`.taggable\_type =  
‘TranslationKey’)) AND (`taggings`.taggable\_id = 8 AND  
`taggings`.taggable\_type = ‘TranslationKey’))  
KeyValue Columns (0.5ms) SHOW FIELDS FROM `key_values`  
KeyValue Create (0.1ms) INSERT INTO `key_values`  
(`translation_key_id`, `translation_value_id`, `created_at`,  
`updated_at`) VALUES(8, 21, ‘2010-05-03 19:47:03’, ‘2010-05-03  
19:47:03’)  
KeyValue Create (0.1ms) INSERT INTO `key_values`  
(`translation_key_id`, `translation_value_id`, `created_at`,  
`updated_at`) VALUES(8, 21, ‘2010-05-03 19:47:03’, ‘2010-05-03  
19:47:03’)  
SQL (2.3ms) COMMIT  
Redirected to [http://0.0.0.0:3000/translation\_keys](http://0.0.0.0:3000/translation_keys)  
Completed in 29ms (DB: 5) | 302 Found [[http://0.0.0.0/](http://0.0.0.0/)  
translation\_values]

Thanks!
