Xclusive
Xclusive

Reputation: 51

before_validation issues with rails

The problem is that I am using

def before_validation
  self.author.strip!
  self.author_email.strip!
end

and I get an error message:

DEPRECATION WARNING: Base#before_validation has been deprecated, please use Base.before_validation :method instead.

Can somebody point me in the right direction. Thanks

Upvotes: 1

Views: 3459

Answers (2)

Scott
Scott

Reputation: 17247

Somewhere toward the top of your class model place the name of your clean-up method:

before_validation :remove_whitespace

... and then further down your model class place a private method with the same name:

def remove_whitespace
  self.author.strip!
  self.author_email.strip!
end

Optionally, if you want a one-liner, then you could also pass a lambda instead of a method name to before_validation:

before_validation lambda {self.author.strip!; self.author_email.strip!}

Upvotes: 9

Doon
Doon

Reputation: 20232

per here http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

Try something like this. you can just call before_validation and pass it a block as opposed to overriding it.

before_validation() do 
  self.author.strip!
  self.author_email.strip!
end

Upvotes: 1

Related Questions