Subscribed unsubscribe Subscribe Subscribe

Augmented Usamimi

it { is_expected.to be_blog.written_by(izumin5210) }

【Ruby】ActiveModel::ModelなクラスでCallbackを使う

ActiveModel::Validations::Callbacksもincludeしなければならない.

class UserForm
  include ActiveModel::Model
  include ActiveModel::Validations::Callbacks

  # 略
end

ActiveModel::Modelに含まれるモジュールは以下のとおり. ActiveModel::Validations::Callbacksは入っていない. 知らんかった.

# activemodel/lib/active_model/model.rb#L58
module Model
  extend ActiveSupport::Concern
  include ActiveModel::AttributeAssignment
  include ActiveModel::Validations
  include ActiveModel::Conversion

  included do
    extend ActiveModel::Naming
    extend ActiveModel::Translation
  end

  # 略
end