What's the correct way to make before_validation, etc. work in an ActiveModel
Asked Answered
R

1

12

Should I extend or include ActiveModel:Validations:Callbacks:ClassMethods or ActiveModel:Validations:Callbacks?

Recompense answered 20/1, 2012 at 4:55 Comment(0)
H
23

I got it to work like this:

class Foo
  extend ActiveModel::Callbacks
  include ActiveModel::Validations
  include ActiveModel::Validations::Callbacks

  before_validation :bar

  def bar
    # callback logic here
  end
end

It's important that you have everything in that order.

Hyperthyroidism answered 27/2, 2012 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.