Suspender 用 1.46.0 可以使用 rails 5.1.6

ThoughtBot 的 rails bootstraper,現在最新版 default 使用了 rails 5.2 很煩,因為我不喜歡 5.2 的 credential 要使用 5.1.x 版本的話要用 v1.46.0 版本的 Suspender,但 1.46.0 是用...

Wayne

bcrypt, devise, and rails secret_key_base

devise 用 secret_key_base 當作產生 token 的依據 devise 用 bcrypt hashify password 然後儲存 rails 的 has_secure_password 也是用 bcrypt 實現 bcrypt() is a h...

Wayne

Devise 每次都會同時下 ORDER BY 和 LIMIT 的 sql 去找 users

同事提出的建議: ``` SELECT "users".* FROM "users" WHERE "users"."id" = $? ORDER BY "users"."id" ASC LIMIT $? id 是 primary key, 理論上不會有重複的資料. ...

Wayne

How to Create Postgres Indexes Concurrently in ActiveRecord Migrations

之前的文章提到要避免 deployment downtime 其中一個方式是 create index concurrently http://everyday1percent.blogspot.com/2018/05/rails-deployment-downtime....

Wayne

新的做 soft delete 的 gem - discard

https://github.com/jhawthorn/discard 沒有 default 加上 default scope 我覺得是很棒的決定,這個 gem 留給開發者更多的彈性實作 soft delete,我覺得在大多數的 case 下都是很夠用...

Wayne

Profiling Rails Boot Time

Infos: https://mildlyinternet.com/code/profiling-rails-boot-time.html https://tenderlovemaking.com/2011/12/05/profiling-rails-startup-w...

Wayne

Rails deployment 如何避免 downtime?

有哪些造成 downtime 的可能性? 1. Code 有問題 2. Migration 跑完後 Web Server 還沒 restart 3. Deploy 完後需要重啟 Server 4. Request 沒有被 queue 起來 可能的解決方式: ...

Wayne

How rails store your migration history

有時候手動操作 database 刪掉了 table,於是乎 rails migration 跟 database 就對不起來了,要怎麼樣騙 rails 說上一個 create table 的 migration file 沒跑過呢?how does rails trac...

Wayne

Rails 不常用但可以變得很實用的 callback

## after_initialize && after_find ```ruby class User < ApplicationRecord after_initialize do |user| puts "You have initialized ...

Wayne

PaperTrail whodunnit find object (ex: find User object)

PaperTrail::Verison object 的 methods 可以去 PaperTrail::VersionConcern 裡面查 查了一下沒有可以找到 whodunnit object 的方法,只好自己加一個: ```rb #initializers/p...

Wayne

rails table_name_prefix

```rb module Snapshot def self.table_name_prefix 'snapshot_' end end ``` ```rb class Snapshot::Schedule < ActiveRecord::Base e...

Wayne

great feature spec example

```rb feature 'User views all books' do scenario 'list all books' do create(:book, title: 'Practical Vim') create(:book, titl...

Wayne

用 foreign_key 確保資料相依正確性 (referential integrity)

Rails 4.2 以後開始支援 database 的 foreign key,很好的文章: https://robots.thoughtbot.com/referential-integrity-with-foreign-keys 我們會用 ```rb class...

Wayne

避免在 migration 裡面用其他 class,最常誤用 model class

為什麼呢?因為 migration 是永久留在那裡的,但是某些 class 是在未來可能被刪掉的,所以一個好的 migration file 要能夠讓 scope 維持在 migration file 裡 > While migrations contain the fu...

Wayne

rails bigdecimal and postgres scale

從 icoinfo 和 otcbtc 上線以來一直被小數點精度問題搞到,原因是因為直接 `.to_d` 的設定和 rails ActiveRecord 的設定不一樣,導致一堆小 bug 假設我們 postgres decimal 欄位設 scale 為 18,那麼假設我要...

Wayne

原來 rails5 以後 ActiveRecord_Collection 可以接收 update 了

原來 rails5 以後 ActiveRecord_Collection 可以接收 update 了 ex: ```rb User.where(is_active: true).update(is_active: false) ``` https://blog.bi...

Wayne

rails csrf token

關於什麼是 CSRF: https://everyday1percent.blogspot.com/2017/12/csrf.html rails 主要就是這裡 ``` def csrf_meta_tags if protect_against_forgery? ...

Wayne

PG::ConnectionBad: could not connect to server

今天遇到一個問題 我安裝 postgresapp 並執行了 server 但是 rails 找不到並噴了下面的 error: ``` ➜ rails db:create could not connect to server: No such file or dir...

Wayne

如何解決安裝 sql 相關 gem 時常常遇到的 error: Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

因為是用 Postgresapp 安裝的 postgres sql,所以常常 bundle install 的時候都會遇到 ``` Gem::Ext::BuildError: ERROR: Failed to build gem native extension. (0...

Wayne

quick start with rails new

筆記一下 rails 一個新的 project 的時候會用到的 commands 我喜歡用: 可以自選版本(side project 喜歡用心一點的版本) 想要選擇要不要 enable webpacker 用 postgresql 不要 unit test,改用 rsp...

Wayne

安全性相關 - CSRF

CSRF 跨站請求偽造,也被稱為 one-click attack 或者 session riding,通常縮寫為 CSRF 或者 XSRF 假設 User A 已經登入了 X 站,因為 A 的瀏覽器有存了 X 站相關的登入紀錄,當 A 進入 Y 站時,Y 站可以偷拿 A...

Wayne

rails before_actoin design pattern - Aspect-oriented_programming (AOP)

想要對 ruby class 實作 before_action ,查到 https://stackoverflow.com/questions/23444964/is-it-possible-to-do-a-before-action-in-ruby-like-in-ra...

Wayne

用 ActiveModel::Callbacks 做 before, around, after callbacks

extend ActiveModel::Callbacks ```ruby class Test extend ActiveModel::Callbacks define_model_callbacks :execute before_execute :log_som...

Wayne

ActiveSupport::Callbacks

延續上一篇,這邊用 ActiveSupport::Callbacks 來做 callbacks 官方教學: http://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html 下一個問題: 所以怎麼做到下...

Wayne

rails 在 database 有 connection 的情況下清空所有 tables

rake db:schema:dump RAILS_ENV=staging rake db:schema:load RAILS_ENV=staging

Wayne