It is quite common to execute multiple queries in a controller action. Just as an example, there might be a dashboard that displays data from multiple models or scopes.
Ruby on Rails
Rails 7 adds Enumerable#maximum and Enumerable#minimum methods to easily calculate the maximum or minimum from extracted elements. These methods have existed in ActiveRecord for some time now and is useful when you have a collection of records.
It looks like a new syntax for declaring enums in your models has been introduced. Instead of passing the name and values as a key/value pair, it can now be passed as two separate arguments instead. Not a big change in itself but what this enables is in my opinion a more intuitive way to pass additional options.
I have written about this or similar topics before in both Post-Deployment script on Elastic Beanstalk: Restart Delayed Job as well as Convenient way to run app commands in Elastic Beanstalk.
Elastic Beanstalk is great! It is very easy to get a Rails app up and running on AWS quickly. Perfect for an app developer that prefers not to deal directly with sysops but still wants to deploy to and leverage the services on AWS.
When the need arise to SSH into your EB instance and execute a command in your app, you might run into issues. I describe in an earlier post how to trigger a restart of DelayedJob after each deployment. In that particular case, one issue was that the command was not running under the correct ruby installation. Turns out that we can make use of EB’s own support scripts to help us out.
When writing controllers in Ruby on rails, using
before_action (used to be called
before_filter in earlier versions) is your bread-and-butter for structuring your business logic in a useful way. It’s what you want to use to “prepare” the data necessary before the action executes.
I have previously written a post about Find or create by and its usages in Rails 3 but Rails have gotten several version updated since then and some of the things I mentioned have even been deprecated at this time, so I wanted to revisit this very useful method again.