核心的find_by()和FinderMethod的find_by()有什么区别? [英] What is the difference with find_by() from the core and the one from the FinderMethods?

查看:63
本文介绍了核心的find_by()和FinderMethod的find_by()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently I'm working on a gem, which overrides ActiveRecords where. By working on that, I stumbled on two different find_by implementations. One is in the core and it uses some kind of cache, whereas the one from the FinderMethods module calls where directly. What is the difference between these two implementations? When is which used?

推荐答案

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

I think it's that way: When you use something like this:

User.find_by(...)

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

The ActiveRecord::Core#find_by is called, as the Core is included into Base from which you inherit.

但是,如果您执行以下操作:

But if you do something like:

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

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

The ActiveRecord::Relation (includes FinderMethods here) will call FinderMethods#find_by

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

I don't know why this is implemented like that, but I'm sure there's a reason for this.

这篇关于核心的find_by()和FinderMethod的find_by()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆