ruby - Rails 3 creates duplicate form object for nested records when updating them -


there 2 models

class category < activerecord::base   has_many :products   accepts_nested_attributes_for :products, allow_destroy: true   validates_associated :products end  class product < activerecord::base   belongs_to :category   validate_uniqueness_of :name, scope: :category_id end 

and form category

<%= simple_form_for @category |f| %>     <%= f.simple_fields_for :products |p| %>       <%= render :partial => "product_fields", :locals => { :f => p } %>     <% end %> <% end %> 

as can see there uniqueness validation makes sure products uniq within category. problem starts when validation not passed.

calling @category.update_attributes(params[:category]) results in duplicate form object. when rendering form errors rails creates additional associated product id of duplicate record different name.

for example:

given have 2 products within category: bread , butter. if set butter bread when editing category's products new form object created , form rendered bread, butter, bread, saying last entry has duplicate name. how can prevent rails creating duplicate records? i'm using rails 3.2.11 if matters.


Comments

Popular posts from this blog

c# - how to use buttonedit in devexpress gridcontrol -

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - System.FormatException' occurred in MongoDB.Bson.dll - XXX is not a valid 24 digit hex string -