accepts_nested_attributes_for 创建重复项
accepts_nested_attributes_for creating duplicates
accepts_nested_attributes_for 创建重复项
型号
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
控制器
def update
@article = Article.find_by_id(params[:id])
@article.update_attributes(params[:article])
redirect_to :index
end
参数
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"}
控制台
Article.find("b8c8ad67-9b98-4705-8b01-8c3f00e55919").article_collections.count
=> 2
问题是每当我们更新文章时都会创建多个 article_collections。
假设 article_collections 是 2,这意味着如果我们正在更新文章,它会创建多个 article_collections = 4,它不会更新相同的 article_collections,而是新创建 article_collections。
为什么会创建重复项?
了解嵌套属性和构建。
在您的编辑操作中构建没有 article_collection.
的对象
例如@article.article_collection.build if @article.article_collection.blank?
。如果它已经有一个文章集合,它不会建立一个新的对象。
您的参数:
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"}
您应该在 "article_collections_attributes" 参数中发送并允许 "id" 属性。例如,
"article_collections_attributes"=>
[
{"id"=>"2", "name"=>"abcd", "type"=>"Special", "description"=>"content ","ordering_type"=>""}
]
我认为这段代码会对你有所帮助。
accepts_nested_attributes_for 创建重复项
型号
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
控制器
def update
@article = Article.find_by_id(params[:id])
@article.update_attributes(params[:article])
redirect_to :index
end
参数
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"}
控制台
Article.find("b8c8ad67-9b98-4705-8b01-8c3f00e55919").article_collections.count
=> 2
问题是每当我们更新文章时都会创建多个 article_collections。
假设 article_collections 是 2,这意味着如果我们正在更新文章,它会创建多个 article_collections = 4,它不会更新相同的 article_collections,而是新创建 article_collections。
为什么会创建重复项?
了解嵌套属性和构建。
在您的编辑操作中构建没有 article_collection.
的对象
例如@article.article_collection.build if @article.article_collection.blank?
。如果它已经有一个文章集合,它不会建立一个新的对象。
您的参数:
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"}
您应该在 "article_collections_attributes" 参数中发送并允许 "id" 属性。例如,
"article_collections_attributes"=>
[
{"id"=>"2", "name"=>"abcd", "type"=>"Special", "description"=>"content ","ordering_type"=>""}
]
我认为这段代码会对你有所帮助。