Using marked.js with hightlight.js in rails to provide github flavored markdown to github style html

In this example, I'll use webpacker to import highlight js and marked js and css file, you can use cdn as well. `app/javascripts/appli...

Wayne

Rails schema 到底怎麼保持乾淨

在不能 access production database 的情況下: 1. 怎麼保持 local database schema 是乾淨的 2. 如果髒了,怎麼恢復成乾淨的而且不影響現在 local 有的資料(乾淨的資料恢復,不乾淨的就丟掉) ## Idea 1...

Wayne

reset password token leaking

reset password 的網址常常是 https://yourdomain/password/reset?token=xxxx 但這樣就會被第三方網站從 `HTTP Referer` 內找到並紀錄起來 reset password 的 token,例如 googl...

Wayne

rails model conditional validation 的神奇用法 extend

lol, 沒想過可以這樣 ```ruby user = User.find(id) user.extend(User::RegistrationContext) ``` ```ruby # app/models/user/registration_context.rb ...

Wayne

Rails where.not 的寫法會略過欄位是 Null 的值

`where.not` 的寫法會略過欄位是 Null 的值,所要對有可能是 Null 值的字串欄位使用 `where.not` 有兩個做法: 1. default 空字串 2. 使用 `or` 另外如果是 boolean 欄位但有可能是 null (雖然一開始就不應該這...

Wayne

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

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

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

Wayne

How rails store your migration history

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

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

quick start with rails new

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

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

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