Today I learned about some of ActiveRecord’s FinderMethods. I was playing around with my database finding student records. I used first, then tried second, and third… they all worked! I was curious so I tried more, unfortunately there’s no sixth method to find the sixth record. It’s important to note that first does not mean find the record with id of “1”, it just means the first record in the database.
Student.first is the same as SELECT * FROM students ORDER BY students.id LIMIT 1.
If you look at the documentation, you’ll see that there’s a forty_two method, to retrieve the forty second record in the database. Yes, it’s a reference to the Hitchhiker’s Guide to the Galaxy!!! 42, or “The Answer to the Ultimate Question of Life, the Universe, and Everything”.
If you want to read the commits and drama:
Part 1: Where DHH adds helper methods from second to tenth. (Check out the comments below!)
Part 2: Where DHH responds to the comments by removing the methods sixth to tenth, and adding forty_two.