簡體   English   中英

Rails HABTM:通過

[英]Rails HABTM :through

我在 3 個不同的商店有產品.. 所有商店可能有相同的產品,但庫存可能不同,價格也是如此.. 我認為 BABTM through 將是最好的解決方案

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 預加載所有內容。 每個 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()

請幫忙?

這有效..我是一個快樂的露營者。 它還將創建和/或更新具有屬性的聯接/關聯表

#!/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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM