gpt4 book ai didi

ruby-on-rails - 核心中的 find_by() 和 FinderMethods 中的 find_by() 有什么区别?

转载 作者:数据小太阳 更新时间:2023-10-29 08:02:57 27 4
gpt4 key购买 nike

目前我正在开发一个 gem,它会覆盖 ActiveRecords where。通过研究,我偶然发现了两种不同的 find_by 实现。一个在 core它使用某种缓存,而来自 FinderMethods 的缓存模块直接调用where。这两种实现有什么区别?什么时候使用?

最佳答案

我认为是这样的:当你使用这样的东西时:

User.find_by(...)

ActiveRecord::Core#find_by 被调用,因为 Core 包含在您继承的 Base 中。

但是如果你这样做:

User.first.products.find_by(...)

ActiveRecord::Relation(包括 FinderMethods here)将调用 FinderMethods#find_by

我不知道为什么要这样实现,但我敢肯定这是有原因的。

关于ruby-on-rails - 核心中的 find_by() 和 FinderMethods 中的 find_by() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38511496/

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