# Validates\_presence\_of fails on observer

**URL:** <https://www.ruby-forum.com/t/validates-presence-of-fails-on-observer/188956>\
**Category:** Rails\
**Created:** [May 21, 2010, 1:49am UTC](https://www.ruby-forum.com/t/validates-presence-of-fails-on-observer/188956 "2010-05-21T01:49:12Z")\
**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:** [May 21, 2010, 1:49am UTC](https://www.ruby-forum.com/t/validates-presence-of-fails-on-observer/188956/1 "2010-05-21T01:49:12Z")

</div>

Hi,

I have the following model that uses the plugin act\_as\_voteable:

class Video \< ActiveRecord::Base  
belongs\_to :user  
acts\_as\_voteable  
acts\_as\_commentable  
attr\_accessible :url, :title, :description, :vote\_count  
validates\_presence\_of :url, :title, :description

…  
end

I also have an observer for Vote model, so each time a user cast a vote  
to a  
video, or other content, I will update some info, including the  
vote\_count  
on video model or other content. The code that runs each time a vote is  
created is:

def update\_user\_points\_and\_votes\_count(vote, factor)  
content\_clazz = Kernel.const\_get(vote.voteable\_type)

```
  content.vote_count +=1*factor
  content.save!
  ...
  ...

```

end

Basically, I will update vote\_count when a vote is casted to a video, or  
other kind of content that act\_as\_voteable.

The problem is that the instruction content.save! raises a validation  
exception on :url (in the list of validates\_presence\_of). I think that  
it  
will fail also on the other attributes in the list of  
validates\_presence\_of.

What I’m missing?

thank you,  
Jp

---

<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:** [May 21, 2010, 5:20pm UTC](https://www.ruby-forum.com/t/validates-presence-of-fails-on-observer/188956/2 "2010-05-21T17:20:35Z")

</div>

Hi, I missed on line in the second block of code:

def update\_user\_points\_and\_votes\_count(vote, factor)  
content\_clazz = Kernel.const\_get(vote.voteable\_type)  
#missing  
content = content\_clazz.fin(vode.voteable\_id)

```
  content.vote_count +=1*factor
  content.save!
  ...
  ...

```

end

Thank you

On Fri, May 21, 2010 at 12:47 AM, Jonhy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> content_clazz = Kernel.const_get(vote.voteable_type)
> 
> ```
> 
> The problem is that the instruction content.save! raises a validation  
> exception on :url (in the list of validates\_presence\_of). I think that it  
> will fail also on the other attributes in the list of validates\_presence\_of.
> 
> What I’m missing?
> 
> thank you,  
> Jp

–  
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:** [May 21, 2010, 5:21pm UTC](https://www.ruby-forum.com/t/validates-presence-of-fails-on-observer/188956/3 "2010-05-21T17:21:10Z")

</div>

Hi, I missed on line in the second block of code:

def update\_user\_points\_and\_votes\_count(vote, factor)  
content\_clazz = Kernel.const\_get(vote.voteable\_type)  
#missing  
content = content\_clazz.fin(vode.voteable\_id)

```
  content.vote_count +=1*factor
  content.save!
  ...
  ...

```

end

Thank you
