rails
config/initializers/json_with_oj.rb あたりに以下のコードを置く class Oj::Encoder def initialize(options) @options = options end def encode(value) ::Oj.dump(value, @options) end end ActiveSupport::JSON::Encoding.json_encoder = Oj::Encoder …
Web アプリケーションのパフォーマンスにうるさいみなさんこんにちは。 Rails アプリのパフォーマンスプロファイリングだと、 rack-mini-profiler が有名で、それ以外だと New Relic とかを使って測定していくのが普通のようですが、物足りない部分があった…
以下の様なクラスがあるとき class Company < AR::Base has_many :employments has_many :engineers, through: :employments, source: :employee, source_type: 'Engineer' end # STI の基底クラス class Employee < AR::Base; end # STI class Engineer < E…
Rails の named scope にデフォルト引数を与えると便利。 class Book < AR::Base scope :published, ->(at = Time.now) { where('published_at > ?', at) } end こうしてデフォルト引数を与えておくと Book.published # => 現在公開済みの本 Book.published …
ちょっと目についたので。Dis りたい訳じゃないです。 これ適切な名前が見つからないとき困るんだよなぁ。あとで思いついて変更なんて作業はしたくないし。割り切ってRoomUser式に統一した方が気が楽だと思う。 http://b.hatena.ne.jp/kensatou/20130512#boo…
今日は Rails での『関連モデル』の名前について考える。 構造としてはこんな感じ。 ・ルーム(Room)に所属するユーザー(User) ・ルーム(Room)での管理者権限を持つユーザー(User) どちらの関連も N:N の関連。いわゆる has_may な感じ。 で、こういう時の命…