如何将产品的分类单元从一种产品复制到另一种产品

How to copy a product's taxons from one product to another

我需要在 Spree (v 2.2) 中使用单个命令行将分类单元从一种产品复制到另一种产品。

一个产品可以有很多分类单元,所以我想这实际上只是将 has_many 关联从一个对象复制到另一个对象的问题。

这是一个糟糕的 sudo 代码想法,逻辑对我来说很有意义:

@product_to_copy_taxons_from = Spree::Product.find(params[:id])

@product_to_copy_taxons_to = @current_product.update_attributes! for taxon_ids: all @product_to_copy_taxons_from.ids

提前致谢!

我相信你想要的是这样的:

@current_product.taxons << @product_to_copy_taxons_from.taxons

您可以在the guide中找到has_many协会添加的方法(包括:<<)。