I am interested in understanding how find_by_(column_name) works.
How does Ruby on Rails dynamically define the method on calling find_by_id, find_by_name, etc?
-
1Possible duplicate of define_method: How to dynamically create methods with argumentsBhojendra Rauniyar– Bhojendra Rauniyar2018-11-02 07:28:39 +00:00Commented Nov 2, 2018 at 7:28
Add a comment
|
1 Answer
This is the power of meta programmation in Ruby, and more specifically the method_missing method: https://ruby-doc.org/core-2.1.0/BasicObject.html#method-i-method_missing
When you call a method on an instance of a Class that is not defined in that class, the method_missing catches it and you can handle it there.