# How to validate against a field that is not in the model?

**URL:** <https://www.ruby-forum.com/t/how-to-validate-against-a-field-that-is-not-in-the-model/148802>\
**Category:** Rails\
**Created:** [September 17, 2008, 8:12am UTC](https://www.ruby-forum.com/t/how-to-validate-against-a-field-that-is-not-in-the-model/148802 "2008-09-17T08:12:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arifb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@arifb](https://www.ruby-forum.com/u/arifb)\
**Post date:** [September 17, 2008, 8:12am UTC](https://www.ruby-forum.com/t/how-to-validate-against-a-field-that-is-not-in-the-model/148802/1 "2008-09-17T08:12:22Z")

</div>

hello all,

i have a model that has a field ‘votes’. users may only vote once per  
session and so i am using a session variable to track whether they  
have voted. i would like to use rails validation within the model to  
output an error if a user tries to vote more than once.

my issue is that this tracking of whether the user has voted this  
session is not tracked in the database (it is in the session  
variable). is it still possible to use the rails validation framework  
to validate? i am doing the appropriate checking in the controller,  
i’m just not sure how to then propagate this check to the model (in  
order to use the validation framework). can i just define a new  
attribute for the model even though it is not a field in the db?

thanks.

---

<div class="post-metadata">

**Author:** ![arifb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@arifb](https://www.ruby-forum.com/u/arifb)\
**Post date:** [September 17, 2008, 8:28am UTC](https://www.ruby-forum.com/t/how-to-validate-against-a-field-that-is-not-in-the-model/148802/2 "2008-09-17T08:28:48Z")

</div>

i realized that the post may have not been totally clear:

basically, i just want to validate against something that is not in  
the db/model - is this possible to do in the model so that i have  
access to the Error object?

---

<div class="post-metadata">

**Author:** ![arifb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@arifb](https://www.ruby-forum.com/u/arifb)\
**Post date:** [September 17, 2008, 10:25am UTC](https://www.ruby-forum.com/t/how-to-validate-against-a-field-that-is-not-in-the-model/148802/3 "2008-09-17T10:25:32Z")

</div>

On Sep 17, 2:28 pm, arifb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i realized that the post may have not been totally clear:
> 
> basically, i just want to validate against something that is not in  
> the db/model - is this possible to do in the model so that i have  
> access to the Error object?

Are you referring to something like this?

class Session  
attr\_accessor :match # match does not exist in the db, that’s why we  
defined a attr\_accessor for it

validates\_presence\_of :match  
end

---

<div class="post-metadata">

**Author:** ![arifb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@arifb](https://www.ruby-forum.com/u/arifb)\
**Post date:** [September 17, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/how-to-validate-against-a-field-that-is-not-in-the-model/148802/4 "2008-09-17T18:33:41Z")

</div>

Why don’t you pass the session value as a parameter to a model’s  
method?

In your controller you would have something like:

model.exceeded\_vote\_count?(session[:vote\_count])

In your model:

def Model.exceeded\_vote\_count?(count)  
count \> MAX\_VOTE\_COUNT ? true : false  
end

Pepe

---

<div class="post-metadata">

**Author:** ![arifb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@arifb](https://www.ruby-forum.com/u/arifb)\
**Post date:** [September 17, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/how-to-validate-against-a-field-that-is-not-in-the-model/148802/5 "2008-09-17T18:36:01Z")

</div>

Sorry, I should have written:

exceeded\_count = Model.exceeded\_vote\_count?(session[:vote\_count])
