【问题标题】:How to copy a product's taxons from one product to another如何将产品的分类单元从一种产品复制到另一种产品
【发布时间】:2019-08-05 21:58:59
【问题描述】:

我需要在 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的事情吗?
  • 嗯...我不知道<< 只是联想复制的强国。就是这样……谢谢。
  • 太棒了。参考指南添加为答案。请根据您的喜好和未来的用户投票/接受。

标签: ruby-on-rails spree


【解决方案1】:

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

@current_product.taxons << @product_to_copy_taxons_from.taxons

您可以在the guide中找到has_many关联添加的方法(包括:&lt;&lt;)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多