问题
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