5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Rails] validationをDBスキーマから自動生成してDRYにする

5
Last updated at Posted at 2016-09-17

DBスキーマでNOT NULLunique、最大長を定義してあるのにModelの方にも

validates :email, presence: true, uniqueness: true
validates :blah, length: { maximum: 255 }

を書くのは面倒だ。DRYじゃない。
…と考える人のためのgemがある。

schema_validations
pretty_validation

pretty_validationはrake db:migrateの後にvalidationのソースファイルが自動生成されるのに対し、schema_validationsはファイルは生成しないがvalidationのコードがauto-magicallyに定義される。
導入はどちらもgem "schema_validations"gem 'pretty_validation'を書くだけだ。

schema_validationは2011年からあり今もちゃんとメンテナンスされているが、githubのスター数が88とあまりメジャーでないようだ。
とても便利そうなのになぜだろう?
使っている人いたら感想を聞かせて下さい。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?