ruby on rails - accepts_nested_attributes_for creating duplicates -


accepts_nested_attributes_for creating duplicates

model

class article < activerecord::base   has_many :article_collections   accepts_nested_attributes_for :article_collections, :allow_destroy => true, reject_if: :all_blank end  class articlecollection < activerecord::base   belongs_to :article end 

controller

def update   @article = article.find_by_id(params[:id])   @article.update_attributes(params[:article])   redirect_to :index end 

params

params = {"utf8"=>"✓"    "article"=>    {     "data_id"=>"dfe9e32c-3e7c-4b33-96b6-53b123d70e7a", "name"=>"mass", "description"=>"mass",     "status"=>"active", "volume"=>"dfg", "issue"=>"srf", "iscode"=>"sdg",          "image"=>{"title"=>"", "caption"=>"",  "root_image_id"=>""},     "article_collections_attributes"=>     [     {"name"=>"abcd", "type"=>"special", "description"=>"content ","ordering_type"=>""}     ]   }, "commit"=>"save", "id"=>"b8c8ad67-9b98-4705-8b01-8c3f00e55919"} 

console

 article.find("b8c8ad67-9b98-4705-8b01-8c3f00e55919").article_collections.count  => 2 

problem whenever updating article it's creating multiple article_collections.

suppose article_collections 2 mean if updating artsicle creating multiple article_collections = 4 , not updating same article_collections, newly creating article_collections.

why creating duplicates.

please us. thanks

your params:

params = {"utf8"=>"✓"    "article"=>    {     "data_id"=>"dfe9e32c-3e7c-4b33-96b6-53b123d70e7a", "name"=>"mass", "description"=>"mass",     "status"=>"active", "volume"=>"dfg", "issue"=>"srf", "iscode"=>"sdg",          "image"=>{"title"=>"", "caption"=>"",  "root_image_id"=>""},     "article_collections_attributes"=>     [     {"name"=>"abcd", "type"=>"special", "description"=>"content ","ordering_type"=>""}     ]   }, "commit"=>"save", "id"=>"b8c8ad67-9b98-4705-8b01-8c3f00e55919"} 

you should send , permit "id" attribute inside "article_collections_attributes" params. example,

 "article_collections_attributes"=>     [     {"id"=>"2", "name"=>"abcd", "type"=>"special", "description"=>"content ","ordering_type"=>""}     ] 

i think code you.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -