I am relatively new to ActiveRecord, which I use alongside Sinatra. Currently, I am building small scale websites. I have not run into any issues with queries taking too long, but I want to know how to optimize searches best, so my site is scalable.
This is something I have been wondering for awhile. Does ActiveRecord treat a query that is broken down into multiple functions different from one that is just one function?
# Single
BlogPost.last(5)
# Broken Down
BlogPost.all.limit(5)
This is more of a conceptual question: how does ActiveRecord run searches? Do more functions added make searches less efficient? When you have a query that is broken down into multiple functions BlogPost.where(publish: true)
- let's say the where(publish: true)
returns fifty results and the limit
limits it to five. Does it first find all fifty publish: true
and then cut off everything after five, or is it smart enough to stop the search after it finds the first five?