Forum: Ruby ActiveRecord dynamic finder methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mark V. (Guest)
on 2006-03-18 00:43
(Received via mailing list)
I've received conflicting answers to this question and I'd like to
confirm the correct answer.

When an ActiveRecord dynamic finder method is invoked, does it create
a new method and save it so the method actually exists the next time
it is called OR does it just evaluate the query based on the method
name every time.

I don't have an opinion on which way is better, I just want to know
how it currently works.
James G. (Guest)
on 2006-03-18 01:04
(Received via mailing list)
On Mar 17, 2006, at 4:41 PM, Mark V. wrote:

> I've received conflicting answers to this question and I'd like to
> confirm the correct answer.
>
> When an ActiveRecord dynamic finder method is invoked, does it create
> a new method and save it so the method actually exists the next time
> it is called OR does it just evaluate the query based on the method
> name every time.
>
> I don't have an opinion on which way is better, I just want to know
> how it currently works.

You can always ask Ruby:

 >> Member.respond_to? :find_by_first_name
=> false
 >> Member.find_by_first_name("James")
=> #<Member:0x2223208 ...>
 >> Member.respond_to? :find_by_first_name
=> false

James Edward G. II
This topic is locked and can not be replied to.