What is the difference with find_by() from the core and the one from the FinderMethods?

China☆狼群 提交于 2019-12-10 18:21:08

问题


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?


回答1:


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

User.find_by(...)

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(...)

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.



来源:https://stackoverflow.com/questions/38511496/what-is-the-difference-with-find-by-from-the-core-and-the-one-from-the-finderm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!