# Update on Observer fails validation

**URL:** <https://www.ruby-forum.com/t/update-on-observer-fails-validation/186966>\
**Category:** Rails\
**Created:** [April 15, 2010, 1:38am UTC](https://www.ruby-forum.com/t/update-on-observer-fails-validation/186966 "2010-04-15T01:38:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonhy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6de8d8/32.png) [@Jonhy\_P](https://www.ruby-forum.com/u/Jonhy_P)\
**Post date:** [April 15, 2010, 1:38am UTC](https://www.ruby-forum.com/t/update-on-observer-fails-validation/186966/1 "2010-04-15T01:38:45Z")

</div>

Hi, I’m puzzzled…

I have this model:

class UserCampaign \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :campaign  
attr\_accessible :is\_active, :user\_id, :campaign\_id

validates\_presence\_of :is\_active, :user\_id, :campaign\_id  
end

and have this observer:

class UserCampaignObserver \< ActiveRecord::Observer

def after\_create(user\_campaign)  
update\_active(user\_campaign)  
end

def after\_update(user\_campaign)  
update\_active(user\_campaign)  
end

private

def update\_active(user\_campaign)  
if user\_campaign.is\_active  
ucs = UserCampaign.find\_all\_by\_user\_id\_and\_is\_active(  
user\_campaign.user.id,true)  
for uc in ucs do  
if uc.id != user\_campaign.id  
uc.is\_active = false  
uc.save!  
end  
end  
end  
end

end

This is simple to guarantee that only one UserCampaign is active.

The problem is that when the observer runs, in uc.save!, the validation  
fails giving me an error saying that the attribute is\_active is  
required…

If I remove is\_required attribute from the list  
of validates\_presence\_of, everything goes fine.

Why it gives me that error saying that is\_active is required when I have  
set  
it just before the save in the observer?

Thanks,

---

<div class="post-metadata">

**Author:** ![Jonhy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6de8d8/32.png) [@Jonhy\_P](https://www.ruby-forum.com/u/Jonhy_P)\
**Post date:** [April 15, 2010, 2:55pm UTC](https://www.ruby-forum.com/t/update-on-observer-fails-validation/186966/2 "2010-04-15T14:55:27Z")

</div>

Just to add more information, I’m using RoR 2.3.5.

Thanks

On Thu, Apr 15, 2010 at 12:36 AM, Jonhy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> def after\_update(user\_campaign)  
> if uc.id != user\_campaign.id
> 
> Thanks,

–  
JoÃ£o Miguel Pereira

> **[jpereira.eu is for sale!](https://www.dropcatch.ai/domains/jpereira.eu)**
>
> This high quality, DA25 domain, first registered in 2006 is for sale for €250. Buy it Now or Make an Offer!

LinkedIn: [http://www.linkedin.com/in/joaomiguelpereira](http://www.linkedin.com/in/joaomiguelpereira)  
removed\_email\_address@domain.invalid  
(351) 96 275 68 58

---

<div class="post-metadata">

**Author:** ![Jonhy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6de8d8/32.png) [@Jonhy\_P](https://www.ruby-forum.com/u/Jonhy_P)\
**Post date:** [April 15, 2010, 3:48pm UTC](https://www.ruby-forum.com/t/update-on-observer-fails-validation/186966/3 "2010-04-15T15:48:08Z")

</div>

validates\_presence\_of checks the attribute according to Object.blank?  
"An object is blank if itâ€˜s false, empty, or a whitespace string. For  
example, “”, " “, nil, [], and {} are blank.”  
When you set a value to false it does not pass the validation.  
If you want to confirm that a value is either true or false you will  
have to it in another way.

Jonhy P. wrote:

> Just to add more information, I’m using RoR 2.3.5.
> 
> Thanks
> 
> On Thu, Apr 15, 2010 at 12:36 AM, Jonhy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > end  
> > def after\_update(user\_campaign)  
> > if uc.id != user\_campaign.id
> > 
> > Thanks,
> 
> –  
> JoÃ£o Miguel Pereira  
> [http://jpereira.eu](http://jpereira.eu)  
> LinkedIn: [http://www.linkedin.com/in/joaomiguelpereira](http://www.linkedin.com/in/joaomiguelpereira)  
> removed\_email\_address@domain.invalid  
> (351) 96 275 68 58
