read only active record model
```rb def readonly? true end ``` https://til.hashrocket.com/posts/7cd7e55929-read-only-models https://stackoverflow.com/questions/56...
rails singleton class
```rb # File activesupport/lib/active_support/core_ext/object/singleton_class.rb, line 3 def singleton_class class << self ...
rails console 的實用技巧
``` reload! app app.get "/movies" helper helper.link_to("Movies", app.movies_path) ``` https://pragmaticstudio.com/blog/2014/3/11...
比較 websocket 和其他技術,以及解釋action cable 的好文
大意 http stateless - polling - long polling - server send event websocket stateful - 2 ways - simultaneous communication - hard to loa...
reddit 上的 rails performance 總結
The Complete Guide to Rails Performance 這本書 https://www.railsspeed.com/ 的總結: * Measure. If you have a serious project a cost of NewRelic...
實做 graphql api 的各種眉角
這篇文章提到了很多實做 graphql api 的各種眉角,都是很 practical 的,值得參考 https://blog.codeship.com/graphql-and-performance-in-rails/
rails 5 better redirect_to :back -> redirect_back(fallback_location: root_path)
redirect_back(fallback_location: root_path) http://blog.bigbinary.com/2016/02/29/rails-5-improves-redirect_to_back-with-redirect-back.h...
DATETIME 和 TIMESTAMP 的差別
這邊說的是 database type 幾個結論 1. datetime 存了 date 和 time,可能用到 8 bit 2. datetime 可以支援的 range 比較廣,從 1000~9999 years 都可以 3. timestamp 則是存從 epoc...
Rails query tips
Exist 滿酷的用法之前沒想過 ```ruby # Post scope :famous, ->{ where("view_count > ?", 1_000) } # User scope :without_famous_post, ->{ where(_no...
怎麼在 rake task 裡面跑 task
最簡單: ```ruby task build: [ "migration:t15349:create_init_version_for_all_accounts", "migration:t15349:migrate_account_version", "migrati...
Rails 5 support CURRENT_TIMESTAMP
rails5 現在 support CURRENT_TIMESTAMP 成為 default value 的寫法,但 annotate_model 的 gem 好像不會自動偵測到 ```ruby class CreatePosts < ActiveRecord::Migr...
rails 可以自己定義 model callback
滿實用的,但應該少用,正常的 lifecycle 能夠滿足就不應該增加新的東西 想到最有可能會用的的地方就是搭配 sub/pub design pattern 的時候,例如 publish 後要 broadcast `published` event http://vi...
devise and warden - 登入失敗寄信
使用 lockable 或是不使用 lockable 都行 `DeviseController#require_no_authentication` 時會 call `warden.authenticate?`,這時會引發 `Lockable#valid_for_aut...
Form object pattern
優點: 1. 把讓 controller 和 view 的邏輯拆開,讓 form object 負責,符合 single responsibility principle 2. 如果 include active_record 可以享用 validations, err...
在 rails 內避免數字因為 race condition 而有誤
例如我有個 Account model 內有 balance column,可以這樣做: ```ruby def increment_balance(amount) self.class.connection.execute "update accoun...
Rails 5.2 似乎要自帶上傳功能囉 (ActiveStorage)
http://afreshcup.com/home/2017/7/6/introduction-to-active-storage.html https://github.com/rails/activestorage
Rails 的 schema.rb file 的用處
https://stackoverflow.com/questions/9884429/rails-what-does-schema-rb-do 簡單來說: 1. production 環境 schema.rb 正不正確都沒差,但錯誤很容易會造成其他 developer...
使用工具檢查有沒有有問題的 gem
很不錯的文章,應該要來試試看 https://philna.sh/blog/2017/07/12/two-tests-you-should-run-against-your-ruby-project-now/
Using Database Replicas in Rails with the Octopus Gem
學習到一些東西 * AWS RDS 可以製作 database 鏡像,讓 reading request 打到這些鏡像可以降低主要 database 的負荷 * Rails access 鏡像的做法似乎有內建的 `config/shards.yml`,這篇文章使用 Oc...