创建行 spree_products_taxons 时显示错误 "uninitialized constant Spree::ProductTaxon"

Show error "uninitialized constant Spree::ProductTaxon" while creating a row spree_products_taxons

在我的应用中,我使用的是 spree gem。我想将 product_id 和 taxon_id 存储在 spree_products_taxons 表中。但是当我创建时,它显示上述错误。我的代码就像

def import
  require 'csv'
  file = params[:file]
  CSV.foreach(file.path, headers: true, encoding:'iso-8859-1:utf-8') do |row|

    @prod = Spree::Product.new()
    @prod.name = row["name"]
    @prod.shipping_category_id = row["shipping_category_id"]
    @prod.description = row["description"]
    @prod.available_on = row["available_on"]
    @prod.meta_description = row["meta_description"]
    @prod.meta_keywords = row["meta_keywords"]
    @prod.tax_category_id = row["tax_category_id"]
    @prod.shipping_category_id = row["shipping_category_id"]
    @prod.promotionable = row["promotionable"]
    @prod.meta_title = row["meta_title"]
    @prod.featured = row["featured"]
    @prod.supplier_id = row["supplier_id"]
    @prod.master.price = row["master_price"]
    @prod.master.cost_price = row["cost_price"]
    @prod.master.sku = row["sku"]
    @prod.master.tax_category_id = row["tax_category_id"]
    @prod.save!
    @prod_taxon = Spree.ProductTaxon.create(taxon_id: row["taxon_id"], product_id: @prod.id)
  end
  redirect_to admin_products_path, notice: "products imported."
end

根据最新的 Spree::Taxon model and Spree::Product model from their GitHub repository,您应该可以替换这部分代码:

@prod.save!
@prod_taxon = Spree.ProductTaxon.create(taxon_id: row["taxon_id"], product_id: @prod.id)

用这个(未经测试)

@prod.save!
@prod_taxon = Spree::Taxon.find(row["taxon_id"])
@prod.taxons << @prod_taxon

希望对您有所帮助!