# Validating a single attribute

**URL:** <https://www.ruby-forum.com/t/validating-a-single-attribute/85106>\
**Category:** Rails\
**Created:** [February 10, 2007, 2:37am UTC](https://www.ruby-forum.com/t/validating-a-single-attribute/85106 "2007-02-10T02:37:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [February 10, 2007, 2:37am UTC](https://www.ruby-forum.com/t/validating-a-single-attribute/85106/1 "2007-02-10T02:37:34Z")

</div>

Is there a handy way to run validation on a single attribute?

I have a form with potentially many objects. The user will enter a  
single number for each of these objects and I’d like to perform  
validation on that entry. But I don’t want to fully validate the object  
because of the associations involved in running a full validation.

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [February 10, 2007, 3:47am UTC](https://www.ruby-forum.com/t/validating-a-single-attribute/85106/2 "2007-02-10T03:47:43Z")

</div>

Jake J. wrote:

> Is there a handy way to run validation on a single attribute?
> 
> I have a form with potentially many objects. The user will enter a  
> single number for each of these objects and I’d like to perform  
> validation on that entry. But I don’t want to fully validate the object  
> because of the associations involved in running a full validation.

Jake,  
As written, your question begs a simple answer…which in this case is  
‘Yes’.

It’s been my experience that I always get more/better help if I include  
some code in my questions.

In this case, we don’t have any names with which to provide you with an  
example of how it would be written. If you send along the schema for  
your object and explain a bit more about how this object appears in your  
form, then we can probably provide better help.

thanks,  
jp

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [February 10, 2007, 6:55am UTC](https://www.ruby-forum.com/t/validating-a-single-attribute/85106/3 "2007-02-10T06:55:34Z")

</div>

Jeff P. wrote:

> Jake J. wrote:
> 
> > Is there a handy way to run validation on a single attribute?
> > 
> > I have a form with potentially many objects. The user will enter a  
> > single number for each of these objects and I’d like to perform  
> > validation on that entry. But I don’t want to fully validate the object  
> > because of the associations involved in running a full validation.
> 
> Jake,  
> As written, your question begs a simple answer…which in this case is  
> ‘Yes’.
> 
> It’s been my experience that I always get more/better help if I include  
> some code in my questions.
> 
> In this case, we don’t have any names with which to provide you with an  
> example of how it would be written. If you send along the schema for  
> your object and explain a bit more about how this object appears in your  
> form, then we can probably provide better help.
> 
> thanks,  
> jp

Well, without adding too much detail, say I have:

validates\_numericality\_of :x  
validates\_associated :a  
validates\_associated :b  
validates\_associated :c

The a, b, c will be setup in advance so can be considered reliable. The  
user will enter ‘x’ and that piece needs to be validated. I don’t want  
to run validation on a, b, c because those make additional database hits  
and have even more database hits when the associations’ associations are  
validated in turn.

Ideally, I’d like something like:

obj.valid?(:only =\> :x)

What I’ve done for the time being is just write a method that sets a  
variable, then I’ve modified my validations as such:

validates\_associated :a, :if =\> !@validate\_only\_x  
validates\_associated :b, :if =\> !@validate\_only\_x  
validates\_associated :c, :if =\> !@validate\_only\_x

It’s not very pretty.
