ActiveRecord query - group, having and count

In has_many relationship, like post has_many comments, we often ask: how many comments are in the post? Or which post has the most comments? ActiveRecord has some methods to do this. Let’s take a look.

Abstract method in Ruby

Abstract method (or interface) defined the method (interface) that should be implemented in the child class. In ruby, there is no abstract method or interface. But we can do some trick to achieve the same goal.

Using rescuable module

ActiveSupport::Rescuable support easier exception handling. That’s often appears in controllers. For example:

Rails Performance Note

Note when reading book The Complete Guild to Rails Performance.

Develop line-bot in local !

Line bot 官方教學介紹了如何把應用部署(deploy)到 Heroku 上,但是開發時如何在本地端(local)測試卻沒有著墨,本文介紹一下如何設定本地端的開發環境,我使用了 sinatra gem as server, rerun gem 來讀取每一次的修改。步驟如下

RSpec benchmark

演算法的寫法百百種,哪一種比較有效率,使用高階程式語言撰寫的話很難直接給出個答案,畢竟 API 內部怎麼實作的就去需要去了解 source code 了,不如直接設置個環境,讓每一種做法去跑跑看(黑箱測試),同樣能得出答案,想說最近在寫 Rspec,就用 Rspec 來寫個 algorithms benchmark 。

RSpec shared_context and shared_examples

撰寫 RSpec 測試時,首先要先設定環境(測試資料),接下來才是撰寫測試腳本,常常會發生不同測試目標(controller, model…)所需的環境雷同(例如都需要建立 user),每個測試目標都要寫一遍十分麻煩,且也不好維護,這時 RSpec 中的 shared_context 就幫上忙了,可以幫我們在不同目標設定測試環境。另外, shared_examples 測是可以在不同的目標中跑同樣的測試腳本,也可以減少相同的測試腳本,提供易維護性。範例:

W3.CSS

使用過許多前端 frameworks, 最早的 Semantic UI, 最火的 Bootstrap, 很潮的 Material-UI (觀望中,尚未使用), 及最近使用的 W3.CSS (本 Blog 就是用它),種種 frameworks 各有優缺點,很難說誰比較好,端看使用情境。

Jekyll on Windows

之前 fork 了 barryclark/jekyll-now,很輕鬆的就能開始自己的 blog ,但 blog theme 的調整總不能一直在 github 上調整,還是 local 測試方便些,就開始嘗試如何在 windows 上跑起 jekyll server。

Rails 後端 React 前端開發環境設置

Rails 後端搭配 React 前端,有不少 gem 可以使用,react-railsreact_on_rails。react-rails 透過 rails 內建的 asset pipeline 完成 react jsx 編譯,react_on_rails 則透過 webpack 來進行編譯,使用上會有些許差異。