# Validates presence

**URL:** <https://discuss.rubyonrails.org/t/validates-presence/61985>\
**Category:** rubyonrails-talk\
**Created:** [December 13, 2011, 4:05pm UTC](https://discuss.rubyonrails.org/t/validates-presence/61985 "2011-12-13T16:05:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Quarite](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Javier\_Quarite](https://discuss.rubyonrails.org/u/Javier_Quarite)\
**Post date:** [December 13, 2011, 4:05pm UTC](https://discuss.rubyonrails.org/t/validates-presence/61985/1 "2011-12-13T16:05:03Z")

</div>

Hi everyone,

I just need a quick help 🙂

What is the best way to write some code in :presence option?

I have a Question model and one of the fields is question\_type: - example question - static question

Each question has alternatives, and each of them has scores

What I want is to validate the presence of :score, but when its only on the static question. I dont want to validate the presence of the score in examples

validates :score, :presence =\> {:if =\> ??? }

What should I do?

Thanks

Javier Q

---

<div class="post-metadata">

**Author:** ![Javier\_Quarite](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Javier\_Quarite](https://discuss.rubyonrails.org/u/Javier_Quarite)\
**Post date:** [December 13, 2011, 4:41pm UTC](https://discuss.rubyonrails.org/t/validates-presence/61985/2 "2011-12-13T16:41:08Z")

</div>

Well I found the code here:

> <https://stackoverflow.com/questions/1673812/rails-validation-for-users-email-only-want-it-to-validate-when-a-user-signs-up>

validates :score, :presence =\> true, :if =\> Proc.new{ (some code here) }

---

<div class="post-metadata">

**Author:** ![Jim\_Ruther\_Nill](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Jim\_Ruther\_Nill](https://discuss.rubyonrails.org/u/Jim_Ruther_Nill)\
**Post date:** [December 14, 2011, 1:16am UTC](https://discuss.rubyonrails.org/t/validates-presence/61985/3 "2011-12-14T01:16:49Z")

</div>

> Well I found the code here:
> 
> [http://stackoverflow.com/questions/1673812/rails-validation-for-users-email-only-want-it-to-validate-when-a-user-signs-up](http://stackoverflow.com/questions/1673812/rails-validation-for-users-email-only-want-it-to-validate-when-a-user-signs-up)
> 
> validates :score, :presence =\> true, :if =\> Proc.new{ (some code
> 
> here) }

while this may work in your case, you should remember the following (a shameless copy

and paste from the api)

Finally, the options `:if`, `:unless`, `:on`, `:allow_blank` and `:allow_nil` can be given to one specific validator, as a hash:

```ruby
validates :password, :presence => { :if => :password_required? }, :confirmation => true

```

Or to all at the same time:

```ruby
validates :password, :presence => true, :confirmation => true, :if => :password_required?

```

one more thing. I find it cleaner to do what you need like this

validates :score, :presence =\> {:if =\> :static?}

def static?

#code here

end

def example?

#code here

end

Cheers!

---

<div class="post-metadata">

**Author:** ![Javier\_Quarite](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Javier\_Quarite](https://discuss.rubyonrails.org/u/Javier_Quarite)\
**Post date:** [December 14, 2011, 2:30am UTC](https://discuss.rubyonrails.org/t/validates-presence/61985/4 "2011-12-14T02:30:10Z")

</div>

Thank! that’s what I found on api, but I wasnt sure if it was the best way of doing that (and by that I mean the easier way to do that “filter”)

I’ll try what you mention because I wasn’t sure enough what":password\_required?" mean ( I tought it was a sort of helper… 🙂 )

Javier Q
