LoginSignup
20
13

More than 5 years have passed since last update.

Rails5からはもうdefault_value_forは必要ないのかもしれない

Last updated at Posted at 2016-07-08

Rails4 -> Rails5 のアップデートでActiveModel::MissingAttributeErrorで引っかかる。

Rails5にアップデートしたところ、FactoryGirlActiveModel::MissingAttributeErrorを出してしまっていた。

最初はなんだかよくわかっていなかったが、どうにもdefault_value_forを利用しているmodelが死んでいるようだった。

Pull Requestは既に出ているが、今のところ取り込まれてない(2016/07/08現在)

どうすれば? -> Rails5で導入されたActiveRecord attributeを使う。

Modelのデフォルト値はattributeで対応出来る。

なので、こんな感じのコードを

class SomeModel < ActiveRecord::Base
  default_value_for :is_public, false
end

こうするだけで良くなった。

class SomeModel < ActiveRecord::Base
  attribute :some_flag, :boolean, default: -> { false }
end
20
13
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
20
13