Rails HABTM:through

问题描述 投票:0回答:1

我在3个不同的商店中都有产品。.所有商店可能都具有相同的产品,但是库存可以不同,价格也可以不同。.我认为通过BABTM是最好的解决方案

class Product < ApplicationRecord
  has_many :store_products
  has_many :stores, through: :store_products
end
class StoreProduct < ApplicationRecord
  belongs_to :store
  belongs_to :product
end
class Store < ApplicationRecord
  has_many :store_products
  has_many :products, through: :store_products
end

我正在尝试创建一个控制台脚本,该脚本使用restfull api预加载所有内容。对于在stores表中预定义的每个store,一个。这些脚本最终将每天运行以捕获所有更改,因此,除了与商店关联创建新产品外,它还应该能够找到并更新它们。我无法使它正常工作

#!/usr/bin/env ruby
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")

  def perform()
    @storeapi   = StoreApi.new
    inventory   = JSON.parse(@storeapi.get_products.body)['data']
    inventory.each do | item |
      sku                     = item['item']['no']
      @item                   = Product.where(sku: sku).first_or_initialize
      @item.weight            = nil
      @item.width             = nil
      @item.depth             = nil
      @item.length            = nil
      unless @item.stores.exists?(name: 'Storename1')
        @item.stores         << Store.where(name: 'Storename1').first
      end
      @item.store_products.update(
        status:             status,
        uid:                item['inventory_id'],
        name:               item['item']['name'],
        quantity:           item['quantity'],
        price:              item['unit_price'],
        data:               item.to_json
      )
      @item.save
    end
  end

perform()

如果我为另一家商店运行类似的脚本,即使它应该是新的,它也会更新store_products。另一个尝试

#!/usr/bin/env ruby
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")

  def perform()
    @magento  = Magento.new
    arg       = {'searchCriteria[page_size]': "10" }
    inventory = @magento.get_products(arg)[:items]
    inventory.each do | item |
      store                   = Store.where(name: 'Storename2').first
      item[:custom_attributes]= Hash[item[:custom_attributes].collect{|a| [a[:attribute_code].to_sym, a[:value]]}]
      item[:stock]            = @magento.get_stockitems(item[:sku])
      sku                     = item[:sku]

      @product                = Product.where(sku: sku).first_or_initialize
      @product.ean            = item[:custom_attributes][:bf_ean]
      @product.weight         = item[:custom_attributes][:bf_weight]
      @product.width          = item[:custom_attributes][:bf_width]
      @product.depth          = item[:custom_attributes][:bf_depth]
      @product.length         = item[:custom_attributes][:bf_length]
      @product.save
      @store_product = StoreProduct.create(
        product:            @product,
        store:              store,
        status:             status,
        uid:                item[:id],
        name:               item[:name],
        quantity:           item[:stock][:quantity],
        price:              item[:price],
        data:               item.to_json
      )
      @store_product.save
    end
  end
perform()

请帮助?

ruby-on-rails associations has-many-through
1个回答
0
投票
这很有效。我是一个快乐的露营者。它还将创建和/或更新具有属性的联接/关联表]

#!/usr/bin/env ruby ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development' require File.expand_path(File.dirname(__FILE__) + "/../../config/environment") def perform() @magento = Magento.new inventory = @magento.get_products()[:items] inventory.each do | item | # define store @s = Store.where(name: 'Brickfever').first # define product @p = Product.where(sku: sku).first_or_initialize @p.update(status: status, ean: item[:custom_attributes][:bf_ean], ... length: item[:custom_attributes][:bf_length], ) # define join table @sp = StoreProduct.where(store: @s, product: @p).first_or_initialize @sp.update(status: status, uid: item[:id], ... data: item.to_json ) end end perform()

© www.soinside.com 2019 - 2024. All rights reserved.