开发者

Rails: How to cache a field of a has_many relationship in an array

开发者 https://www.devze.com 2023-03-06 17:18 出处:网络
Here is what I\'m doing right now: m = Merchant.find(1) ebay_item_id_array = [] active_items = m.active_items

Here is what I'm doing right now:

m = Merchant.find(1)
ebay_item_id_array = []
active_items = m.active_items
active_items.each { |item| 
  # Fill the array containing what items are already in the DB based on ebay item id
  ebay_item_id_array.push(item.external_product_id)
}

The goal here is so that when I ping eBay's API for the latest products, I can check if I already have that ebay item id in my database. Thus, it would be skipped. The pr开发者_高级运维oblem is that when the DB gets big, this operation above is not very efficient. How can I cache the external_product_field into an array that I can bring up without looping across all my active records?


You should consider polling the eBay API for products added after the last update of your database that would be less computationally expensive. You can do this by using itemFilters

See the documentation for more info: http://developer.ebay.com/DevZone/finding/CallRef/findItemsByKeywords.html#Request.aspectFilter

0

精彩评论

暂无评论...
验证码 换一张
取 消