RSSAmplifier

Blog

Lynn's Dev Blog

Lynn's Dev Blog

lynnbright.comRSS feed ↗20 posts

Latest posts

Local PostgreSQL Upgrade and Database Restoration Guide

PostgreSQL Upgrade and Database Restoration Guide To support the pgvector gem, I upgraded my local Postgres.app from v13 to v17 on macOS. Below is the full upgrade and restoration process for my project. Environment OS: macOS PostgreSQL: Upgraded f...

Resolving Rails Runtime Errors: A Guide to Handling Unpersisted Changes with with_lock

Introduction In Ruby on Rails applications, handling database transactions efficiently is crucial to maintaining data integrity. One common issue developers face is dealing with runtime errors caused by unpersisted changes when using the with_lock me...

Upgrade to Ruby 3.0.6 (OpenSSL issues)

Environment MacOS Sonoma 14.3 Original Ruby version: 2.7.8 Ruby Version Manager: RVM 1.29.12 Goal Upgrade Ruby from 2.7.8 to 3.0.6. Issues Description After successfully installing Ruby 3.0.6, I encountered difficulties running bundle install in...

What’s the relationship between require, $LOAD_PATH and $LOADED_FEATURES in Ruby?

It is common to see require 'something' in a Rails application. But what exactly does require do? What is require used for in Ruby? require is used to load external libraries or modules into your program. For example, Before you require the JSON modu...

How I learned to write raw SQL

In my daily job, I mainly use Rails framework. To make code readable and maintainable, we seldom write raw SQL in the codebase. Instead, we use Rails's ActiveRecord::QueryMethods module which helps developers write beautiful queries quickly. For exam...

7 steps I usually do before I involve in a complex feature implementation

Before implementing a new feature, I like to make sure what problems we are going to solve, why we should solve them, and how much time we can implement this feature. Here are 7 steps I usually do before I involve in a complex feature implementation....

What's the difference between find_each, find_in_batches, in_batches in Rails?

Sometimes we will use klass_name.all to get records from the database. That’ll be ok, if we only have 100 records in the database. However, using klass_name.all might not be the best way to get records, especially when we need to query large numbers ...

VS Code x Rubocop - ruby_executable_hooks: No such file or directory

Environment macOS Monterey (Version 12.1) Ruby 2.6.6 Ruby version manager: RVM Editor: VS Code To Reproduce: I've installed rubocop gem in my app and add an extension ruby-rubocop in my VS Code. Open VS Code and see the warning messages below. ...

How to define a multiline string in Ruby

Note: The following was tested with Ruby 2.6.6 Keyword: Heredoc What is heredoc? Heredoc is used for a form of multiline strings and preserves the line breaks and whitespace (including indentation) in the text. How to define a heredoc in Ruby? stri...

How to turn on the expanded table formatting mode in PostgreSQL

Note: The following was tested with PostgreSQL 13.1 Default setting: \x off $ psql myblog_dev myblog_dev=# SELECT "users".* FROM "users" WHERE "users"."role" = 10 LIMIT 1; The output format would be: It was difficult to read the result. Turn on th...

6 tips to ensure your rake task runs smoothly

Rake is a task runner/task management tool in Ruby. You can create diverse tasks, put tasks in the Rakefile, and execute a command like rake :your_awesome_task. Your task will start running by Rake. In Ruby, you can put task code inside a file named ...

How to Benchmark my code in Ruby or Rails

Recently, I had a job to fix the slow code. Before diving into fixing the slow code, I used Scout APM to identify the performance bottlenecks, which helped me make sure where the slow code is. To prove the new optimization solution is faster, in addi...

Lessons learned from failing to access an environment variable in YMAL file

Note: The following was tested with Ruby 2.6.6 / Rails 5.2.6. Situation I tried to add an environment variable in the application.yml file. # config/application.yml default: #... API_TOKEN: ABC123456 And I expected when executing ENV['API_TOKEN...

When ORDER BY and NULL work together

When we would like to sort the result-set in ascending or descending, we can use ORDER BY in SQL query. In Rails, we can use order which is derived from ActiveRecord::QueryMethods. For example, Article.all.order(published_at: :desc) Let's focus on t...

How to use and test an AJAX request in Rails

What's AJAX? AJAX (Asynchronous Javascript and XML) is a kind of technique that can send HTTP requests and exchange data with a server by Javascript. It's an important and practical technique to enhance the user experience, especially when we want to...

Understand `nil` in Ruby

What's nil? nil is an instance of NilClass. It's a special Ruby object used to represent the absence of any value. And it also behaves like false when used in a conditional statement. What's more, there is only one nil object, whose object_id is alwa...

3 helpful methods in Rails console

Recently, I start reading Practicing Rails. I love the main point in chapter 1: Rails console is a good place to experiment with our ideas. There are 3 tips to explore ideas through the Rails console I learned from Practicing Rails. 1. Use app object...

RSpec - Use shared_examples to avoid duplicate test examples

It's common to see a before_action method that need executing in different controller actions. At the same time, when writing RSpec tests for these actions, it's easily to write down context blocks whose test examples are almost the same, and only a ...

Before adding a new validation, one thing you should notice

When we develop a new feature, it's common to add a new column that needs some validations in the model. So, we might write something like this: class Book < ApplicationRecord #... validates :barcode, presence: true end To increase the test cove...

Git - There are too many unreachable loose objects

To Reproduce: git cm 'some_message' commit successfully, but git showed the message below: error: The last gc run reported the following. Please correct the root cause and remove gc.log. Automatic cleanup will not be performed until the file is r...