Duplicate inserts with field_for and has_many :through

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
Completed in 29ms (DB: 5) | 302 Found [http://0.0.0.0/
translation_values]

Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs