0

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?

sawa
  • 165,429
  • 45
  • 277
  • 381
  • 1
    Possible duplicate of [define\_method: How to dynamically create methods with arguments](https://stackoverflow.com/questions/38233060/define-method-how-to-dynamically-create-methods-with-arguments) – Bhojendra Rauniyar Nov 02 '18 at 07:28

1 Answers1

2

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.

Vincent Rolea
  • 1,601
  • 1
  • 15
  • 26