Rails nested transaction

TL:DR: Best Practice: 在 nested transaction 內不要手動 `raise ActiveRecord::Rollback`, 一律使用 `save!`, `update!` 等操作,就不會有問題。 1. raise `ActiveRe...

Wayne

How to extend ActiveRecord

Let's have a simple example, I want to implement a `puts_something` callback to `ActiveRecord`, whenever my object get initialzied, I wan...

Wayne

Different behaviour about counter_cache between Rails6.x and Rails5.x

TL;DR: ```rb update!(somthing_id: somthing.id) # => This line will trigger counter_cache in Rails5 but not in Rails6 ``` To make it w...

Wayne

How DHH write RESTful controller

常常要貼這個連結給別人但每次都忘記這篇的連結在哪,紀錄一下以後好搜尋 https://gist.github.com/dhh/10022098

Wayne

ActiveStorage#attach has different return value between v5 and v6

In activestorage v5 ```rb current_user.images.attach( io: file, filename: file.original_filename, content_type: file.content_type ...

Wayne

We can change ActiveStorage route_prefix in Rails 6

Usage: ```rb # config/application.rb config.active_storage.routes_prefix = '/files' ``` So instead of having route like `www.example.c...

Wayne

Finally we can disable generated scope from ActiveRecord#enum in Rails 6

Usage: ```rb class Post < ApplicationRecord enum state: { draft: 0, published: 1, personal: 2 }, _scopes: false end ``` PR: https://g...

Wayne

Validate JSON schema in Rails

This is an archive of my post from 2017 (revised by [@morhekil](https://twitter.com/morhekil)), original posted in [medium](https://mediu...

Wayne

Using RabbitMQ and Hutch with RPC call for queue in Rails

This is an example how the author implement the RPC call using Hutch and communicate with RabbitMQ https://karolgalanciak.com/blog/2020/...

Wayne

一篇在講 Ruby on Rails 的 ActiveRecord 和 Elixir Phoenix Ecto 差別的文章

一篇在講 Rails 的 ActiveRecord 和 Phoenix Ecto 差別的文章,其實主要就是表達 Model 跟 Schema 脫鉤這樣而已,另外也提到了 Model 沒有 State 所以可以更好 passing around, ex: ```elixir...

Wayne

How to test with Salesforce Heroku Connect - Part 2

In my previous post [How to test with Salesforce Heroku Connect ](https://waynechu.cc/posts/290-how-to-test-with-salesforce-heroku-conne...

Wayne

Rails 用 webpacker 時 compile 很慢而且很吃 memory?You're probably a over-packer lol

長話短說,就是 webpacker 會 compile **所有** 在 `japascripts/packs` 資料夾底下的東西,所以只要沒有要在 layout 裡面 include 的 file 就不要放在 packs 下面,這樣就會變快了。 Copy paste f...

Wayne

Faking External Services in Tests with Adapters

Faking External Services in Tests with Adapters https://thoughtbot.com/blog/faking-external-services-in-tests-with-adapters

Wayne

Unexpected behaviour for ActiveModel::Model validate and valid?

Assume we have an `FakeUser` class looks like below: ```rb class FakeUser include ActiveModel::Model include ActiveModel::Attribute...

Wayne

10 個 ActiveRecord 的新功能

快速看看,有印象就好 https://hint.io/blog/10-New-Things-in-Active-Record

Wayne

Rails 6 ActiveStorage updates

工作上有用到 ActiveStorage,然後我又手癢想升級 Rails,所以先記錄一下 https://blog.saeloun.com/2019/11/04/rails-6-active-storage-updates.html

Wayne

Finally we have multi env credential in Rails 6

In Rails 6, default credential is for development and staging, they'll store in the same path ``` config/credentials.yml.enc config/mast...

Wayne

When to use Hash and when to use HashWithIndifferentAccess and why

## TL;DR: * When passing to our class: Both works, because we control everything * When passing to 3rd party library: Hash is better, we...

Wayne

Rails 6 released

https://weblog.rubyonrails.org/2019/8/15/Rails-6-0-final-release/

Wayne

Rails ActiveModel with type casting

## Basic usage Use `include ActiveModel::Model` and `include ActiveModel::Attributes` can provide type casting for active model. Exampl...

Wayne

How to test ActionMailer with RSpec

This blog post will demostrate how to test ActionMailer in Rails using RSpec. ## Configuration ```rb # config/environments/test.rb # T...

Wayne

Rails lock! and with_lock

Basically, `ActiveRecord::Locking::Pessimistic#with_lock` will yield your code inside transaction but `ActiveRecord::Locking::Pessimistic...

Wayne

How to test Stripe webhook's singature with Rspec

This post shows how to test Stripe webhook's singature ```rb # spec/support/stripe_test_helper.rb module StripeTestHelper def stripe_...

Wayne

How to split rails routes.rb into smaller files - Extend

I've tried many ways to split `config/routes.rb` file, I find this approach is best to me because it requires no futher config to let dev...

Wayne

How to split rails routes.rb into smaller files - require

Anothr approach: use `require` In `config/routes.rb` ```rb Rails.application.routes.draw do root to: "home#index" get "/about get...

Wayne