# ActiveRecord object.valid? triggers unexepected results

**URL:** <https://www.ruby-forum.com/t/activerecord-object-valid-triggers-unexepected-results/127962>\
**Category:** Rails\
**Created:** [February 3, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/activerecord-object-valid-triggers-unexepected-results/127962 "2008-02-03T12:26:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [February 3, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/activerecord-object-valid-triggers-unexepected-results/127962/1 "2008-02-03T12:26:43Z")

</div>

I’m editing an ActiveRecord object in a session through AJAX, updating  
it with object.attributes = params[:object], and validating it with  
object.valid?  
Because it’s associated with other objects in a form, I want to save  
them all together.

The surprise was that object.valid? triggers after\_validation\_on\_update.  
I’m having an audit log and I was getting bogus entries.

> **[ActiveRecord::Validations](https://api.rubyonrails.org/classes/ActiveRecord/Validations.html#M001316)**
>
> Active Record Validations Active Record includes the majority of its validations from ActiveModel::Validations all of which accept the :on argument to define the context where the validations are act.

valid?()  
Runs validate and validate\_on\_create or validate\_on\_update and returns  
true if no errors were added otherwise false.

valid?() should run all validations, but not trigger after\_validation  
callbacks.  
This is not just a rant… I’m looking for solutions? How can I have an  
audit log on updates and running valid()? without polluting my audit  
log?

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [February 3, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/activerecord-object-valid-triggers-unexepected-results/127962/2 "2008-02-03T12:49:45Z")

</div>

Constantin G. wrote:

> How can I have an  
> audit log on updates and running valid()? without polluting my audit  
> log?

Well… I gave my own answer. Replace after\_validation\_on\_update with  
before\_update. I think the misunderstanding came from the fact that I  
interpreted “after\_validation on\_update” when it actually was “after  
validation\_on\_update”.
