我一直在努力找出如何避免使用这段代码中的这么多SQL查询。我通过Shopify的API接收Collect对象,然后检查本地数据库中是否已经有匹配项,如果没有,则使用本地匹配产品和集合中的参数创建一个新的本地Collect对象。我不确定如何使用includes或join,因为我正在进行查找,而不仅仅是在循环中获取值。欢迎任何指点。
def seed_collects!
shop = self.with_shopify!
c = (1..(ShopifyAPI::Collect.count.to_f/250.0).ceil).flat_map do |page|
ShopifyAPI::Collect.find(:all, :params => {:page => page.to_i, :limit => 150})
end
c.each do |collect|
next if Collect.find_by(shopify_collect_id: collect.id)
product = Product.find_by(shopify_product_id: collect.product_id.to_s)
collection = Collection.find_by(shopify_collection_id: collect.collection_id.to_s)
col = Collect.new(shopify_collect_id: collect.id,
position: collect.position,
product_id: product.id,
collection_id: collection.id)
col.save
puts col.errors.inspect if col.errors.any?
end
end
架构
create_table "collections", force: :cascade do |t|
t.string "title"
t.string "shopify_collection_id"
t.string "collection_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "shop_id"
end
create_table "collects", force: :cascade do |t|
t.string "position"
t.string "shopify_collect_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "collection_id"
t.integer "product_id"
end
create_table "products", force: :cascade do |t|
t.string "title"
t.string "shopify_product_id"
t.string "product_type"
t.string "tags"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "shop_id"
t.string "main_image_src"
end
模型
class Collect < ActiveRecord::Base
belongs_to :product
belongs_to :collection
delegate :shop, to: :product
delegate :shopify_product_id, to: :product
delegate :shopify_collection_id, to: :collection
end
class Collection < ActiveRecord::Base
# verify all items belong to shop
belongs_to :shop #verify
has_many :collects, dependent: :destroy
has_many :products, through: :collects
end
class Product < ActiveRecord::Base
belongs_to :shop
has_many :variants, ->{ order(:created_at) }, dependent: :destroy
has_many :price_tests, dependent: :destroy
has_many :metrics, ->{ order(:created_at) }, dependent: :destroy
has_many :collects, dependent: :destroy
has_many :collections, through: :collects