PgIndexWhereをRubyGems.orgでリリースしました
PostgreSQLでユニークインデックスを指定するときwhereを指定したい話の続きの続きです。
こんな感じでRailsのMigrationファイルで:whereが指定できます。
class AddIndex < ActiveRecord::Migration def up add_index "customers", ["code"], :name => "customers_idx01", :unique => true, :where => 'deleted_at is null' end def down remove_index "customers", :name => "customers_idx01" end end
これをやると何が嬉しいかは以前の記事「acts_as_paranoid + ユニークインデックス」を御覧ください。
RailsのModel側だけでバリデーションを設定するより、DB側でもユニークインデックスを設定しておいたほうが安全です。PostgreSQL + acts_as_paranoidの人は是非導入してみて下さい。