gpt4 book ai didi

ruby-on-rails - 如何优化对已更改对象及其依赖项记录的搜索

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:25 25 4
gpt4 key购买 nike

我正在搜索自给定日期以来发生更改的事件记录对象。我下面的代码有效,但我想更有效地进行这些调用。有什么想法吗?

 # product_controller.rb file
@products = products.select {|product| product.any_update_since(update_date)}

# product.rb file
def any_update_since(date)
return true if self.updated_since(date) ||
self.specs.any?{|t| t.updated_since(date)} ||
self.content.any?{|t| t.updated_since(date)} ||
self.images.any?{|t| t.updated_since(date)}
return false
end

def updated_since(date)
Time.zone = 'UTC'
update_date = Time.zone.parse(date)
return true if (self.updated_at > update_date)
return true if (self.translations.any?{|t| t.updated_at > update_date})
return false
end

最佳答案

如果这些是事件记录关联,您可以完全在数据库层执行此操作:

products
.joins(:specs, :content, :images)
.where('products.updated_at > :date OR specs.updated_at > :date OR contents.updated_at > :date OR images.updated_at > :date', date: update_date)


products
.joins(:translations)
.where('products.updated_at > :date OR translations.updated_at > :date', date: update_date)

关于ruby-on-rails - 如何优化对已更改对象及其依赖项记录的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57858246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com