The Rails guide says this about the database connection pool
Any one request will check out a connection the first time it requires access to the database. At the end of the request it will check the connection back in. This means that the additional connection slot will be available again for the next request in the queue.
I take this to mean that it is best practice within a controller action to push DB queries and saves to the end of the action so the connection is held for a shorter period of time.
For example, if I have an action that uploads some images sent from the client to another server and then saves the location of those images and some other data to the DB then it’d be better to do all the image uploads first before making any DB queries. Is this generally right?
Furthermore, is it generally best practice to have more complicated model methods avoid calling
save or any other DB mutating methods so that they can be composed together in a single or small number of
save calls at that end?