简体   繁体   English

Rails HABTM:通过

[英]Rails HABTM :through

I have products in 3 different stores.. All stores may have the same products but the inventory can be different and so is the price.. I thought BABTM through would be the best solution我在 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

I am trying to create a console script which preloads everything using a restfull api.我正在尝试创建一个控制台脚本,该脚本使用 restfull api 预加载所有内容。 one for each store which has been predefined in the stores table.每个 store 表中已预定义的商店。 These scripts will eventually be run every day to catch all changes so besides creating new products in association with a store it should also be able to find and update them.这些脚本最终将每天运行以捕获所有更改,因此除了创建与商店关联的新产品之外,它还应该能够找到和更新它们。 I can't get it to work我无法让它工作

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

If I run a similar script for another store it will update store_products even though it should be a new one.如果我为另一家商店运行类似的脚本,它会更新 store_products,即使它应该是一个新的。 Another try另一个尝试

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

Please help?请帮忙?

this works.. I am a happy camper.这有效..我是一个快乐的露营者。 It will create and or update joined/associated tables with attributes as well它还将创建和/或更新具有属性的联接/关联表

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