Robin
Robin

Reputation: 8498

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

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?

Upvotes: 3

Views: 126

Answers (1)

siegy22
siegy22

Reputation: 4413

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.

Upvotes: 2

Related Questions