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

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?


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

Why don't you pass the session value as a parameter to a model's

In your controller you would have something like:


In your model:

def Model.exceeded_vote_count?(count)
  count > MAX_VOTE_COUNT ? true : false


Sorry, I should have written:

exceeded_count = Model.exceeded_vote_count?(session[:vote_count])