Mike Coutermarsh
  • Home
  • About
  • Twitter
  • GitHub
Sign in Subscribe

How to use Flash messages from a Rails API only controller

If you are debugging this Flash message error. undefined local variable or method `flash' for And you are currently using an API only Rails controller, but still want to use flash messages in your app because you are redirecting to a normal controller. Then you can fix this by adding
Aug 16, 2023

How to stop Rails from setting a session cookie

Recently I was working on a Rails API and I wanted to stop Rails from setting it's _session cookie for API requests. Since these requests are authenticated via an API token, there's no reason for the cookie to be returned to the user. # application_controller.rb after_action lambda { request.
Aug 11, 2023

How to make a Fly API call from Ruby

Fly has a graphql API available. You can find ways to use it by looking through their CLI's source code. I recently wanted to automate a Fly API call from a ruby script. And I was not in an environment where I wanted to install the fly CLI. Here's some
Jul 31, 2023 1 min read

Setting an Actions secret with Octokit.rb - Ruby Example

Here's a Ruby example of how to set a GitHub Actions secret using Octokit in Ruby. The secret needs to be encrypted before it is sent to the API. You need the octokit and rbnacl gems installed. require "octokit" require "rbnacl" client = Octokit::Client.new(access_token: ACCESS_TOKEN) value
Jul 27, 2023 1 min read

Performant database tree traversal with Rails

New post over on the PlanetScale blog. This was the toughest N+1 performance problem I've faced. It shows a trick you can use to preload records, in a situation where includes doesn't cut it. Go check it out.
Jul 12, 2023

How to tech lead and survive

If you're in a tech lead role and struggling. Try this. Make a mindset shift. Ask yourself: "How can I not be the person who builds that?" This will feel hard because for years you have been good at building. You're used to taking on the hardest problems. You cannot
Jul 10, 2023 1 min read

Fly + PlanetScale + Rails example dockerfile

If you are debugging this error: LoadError: libmariadb.so.3: cannot open shared object file: No such file or directory - /rails/vendor/bundle/ruby/2.7.0/gems/mysql2-0.5.5/lib/mysql2/mysql2.so The fix is having these packages installed for production: default-mysql-client default-libmysqlclient-dev Here's a full
Jun 10, 2023 2 min read

Sidekiq: retry once before erroring

Here's how you can silence/mute the initial Sidekiq error, but still report any errors after the first try. This reduces noise in the error tracker and makes any errors more actionable. def perform # Do work rescue SomeError => e retry_once_before_raising_error(e) end Read on to implement
Jun 9, 2023 2 min read

Rails tests: Content-Length header before sending it

Here is a quick and easy way to estimate the content-length of a request from the params being sent. I needed to do this recently in a Rails test. require 'rack' params = { mike: true } params_string = Rack::Utils.build_nested_query(params) content_length = params_string.length puts content_length
May 30, 2023

Using Multi-Node Elasticache with Rails

When using multi-node Elasticache memcached with Rails, it's critically important to get your setup correct. If you don't, your application will connect to memcached nodes at random (!!!). Resulting in low cache hit rates. How to setup Conveniently, there is a gem for this, dalli-elasticache. gem "dalli-elasticache" Using the gem for
May 4, 2023 1 min read

Sidekiq connecting logs in test

Sidekiq 7.0.6 connecting to Redis with options If you have these all over your Rails test logs, here is how you can disable them for test. In test/test_helper.rb, add the following: Sidekiq.logger.level = Logger::WARN Now your test logs will be clean. This took
Apr 21, 2023

New index name generation in Rails 7.1

For as long as I can remember Rails has had this little quirk where it will auto generate index names that are too long. A couple weeks ago Andrew Culver tweeted out his wish for this to be improved. I'll contribute $500 to the bounty for anyone who is willing
Apr 8, 2023 1 min read

Top 5 Ruby gems for Rails apps

I recently shared a couple of my favorite gems on Twitter. And people asked for more! These are my top 5 must have gems for any Rails app + my reasoning and some tips on how to best use them. 1. Pry 2. Flipper 3. Sidekiq 4. Rack::Attack 5. Prometheus
Apr 2, 2023 4 min read

No fear Rails schema changes

Over on the PlanetScale blog I just wrote up how we handle schema changes in our own Rails app. It's the best workflow I've ever used for Rails migrations. It's both inspired by and better than the custom tooling I had access to when working for GitHub. Click the big
Apr 2, 2023 1 min read

There was an error parsing Gemfile: windows is not a valid platform

I hit this error recently. To fix this, you need to update to a newer version of bundler. gem update bundler Good luck, hope this saved you some time. Error There was an error parsing `Gemfile`: `windows` is not a valid platform.
Mar 23, 2023

Trick for fixing Rails `find_by` N+1's

Recently I had some code that was doing 100's of find_by queries. Due to the way it was setup, a simple fix using includes wasn't possible. I was able to know which records would be called via find_by though, meaning I should be able to preload all the
Feb 26, 2023 2 min read

New Rails health check endpoint

Rails just recently added a default /up route that will return a 200 when the application is running. I've had to implement this in pretty much every Rails app I've ever worked on. This is a nice addition! If you're not familiar. This is useful for load balancers which need
Jan 13, 2023 1 min read

Solving N+1’s with Rails `exists?` queries

I recently solved a Rails performance issue with exists? queries in our Rails API. Shared all the details on PlanetScale's blog. Check it out here: Solving N+1's with Rails exists queries.
Jan 10, 2023

Searching complex Rails routes

Mastering the routes command will save you a lot of time. Here are a pro tips on how to use it. rails routes Prefix Verb URI Pattern Controller#Action new_user_session GET /sign-in(.:format) users/sessions#new {:subdomain=>"auth"} user_session POST /sign-in(.:format)
Jan 7, 2023 1 min read

Local Dev for a Ghost Theme

I recently customized the default Ghost Casper theme for this blog. It took me a while to figure out a good local development flow. Here is what I ended up with. 1. Install Ghost locally First, you need to be running a Ghost instance locally. Instructions for doing this are
Jan 6, 2023 1 min read

Rails + MySQL: Querying JSON

Here's how to use ActiveRecord to query a JSON column in MySQL. In this example we have a json column user_settings. We want to find which records have theme set with a value. Model.where("JSON_EXTRACT(user_settings, '$.theme') IS NOT NULL").count JSON_EXTRACT is the
Jan 2, 2023
PlanetScale

Building a multi-region Rails application with PlanetScale

View this post on PlanetScale.
Dec 8, 2022
PlanetScale

Introducing FastPage: Faster offset pagination for Rails apps

View this post on PlanetScale.
Aug 16, 2022
PlanetScale

Ruby on Rails: 3 tips for deleting data at scale

View this post on PlanetScale.
Aug 1, 2022
PlanetScale

Deploy requests now alert on potential unwanted changes

View this post on PlanetScale.
Jul 6, 2022
Page 1 of 5 Older Posts →
Mike Coutermarsh © 2023
  • HTML/CSS to Image API
  • Twitter
  • GitHub
  • LinkedIn