# Best way to do model validation and pass errors?

**URL:** <https://www.ruby-forum.com/t/best-way-to-do-model-validation-and-pass-errors/161688>\
**Category:** Rails\
**Created:** [March 1, 2009, 12:15am UTC](https://www.ruby-forum.com/t/best-way-to-do-model-validation-and-pass-errors/161688 "2009-03-01T00:15:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![openmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6de8d8/32.png) [@openmind](https://www.ruby-forum.com/u/openmind)\
**Post date:** [March 1, 2009, 12:15am UTC](https://www.ruby-forum.com/t/best-way-to-do-model-validation-and-pass-errors/161688/1 "2009-03-01T00:15:34Z")

</div>

Hi all,

Following up on the best practices series, what is the best way to do  
some more advanced model validation, beyond presence, numericality, etc.  
For example, I want to do some checks on the database to see if a user  
has performed a certain action before (such as joining a group), and  
then pass the appropriate error messages back to the controller?

Having followed the best practices to move as much logic out of the  
controller and into the model, I have created a function  
Group.add\_user(user) that checks for existing membership in the group,  
and then makes the association. However, I’d like to be able to pass an  
error message back into the flash notice if the user has already joined  
the group. I can think of two ways of doing this…

1. 

In controller:  
if group.check\_ownership(user)  
Group.add\_user(user)  
else  
flash[:notice] = “error message”  
end

I don’t like this method, because it seems to be putting logic back into  
the controller; this logic could get more complex outside of this simple  
example.

1. 

In Group model:  
def add\_user  
if !group.check\_ownership(user)  
Group.add\_user(user)  
else  
return “error message” # or return an error number  
end  
end

1. 

Write a validator? A brief read in DHH’s book seems to suggest  
validators are more for checking database content than for things like  
checking associations.

Is there a better method? Any help much appreciated!
