# Validate has\_many relationship with conditions?

**URL:** <https://www.ruby-forum.com/t/validate-has-many-relationship-with-conditions/145792>\
**Category:** Rails\
**Created:** [August 13, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/validate-has-many-relationship-with-conditions/145792 "2008-08-13T23:45:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [August 13, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/validate-has-many-relationship-with-conditions/145792/1 "2008-08-13T23:45:39Z")

</div>

I need to validate that I have at least one relationship with a certain  
condition. My code so far:

class User \< ActiveRecord::Base  
belongs\_to :user\_type  
end

class UserType \< ActiveRecord::Base  
has\_many :users  
has\_many :admins, :source =\> :users, :conditions =\> “user\_type\_id = 1”  
end

How do I validate that I have at least one admin when updating any user?

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [August 13, 2008, 11:58pm UTC](https://www.ruby-forum.com/t/validate-has-many-relationship-with-conditions/145792/2 "2008-08-13T23:58:28Z")

</div>

You can overwrite validate\_on\_update in your User model.

> **[ActiveRecord::Validations](https://api.rubyonrails.org/classes/ActiveRecord/Validations.html#M001044)**
>
> Active Record Validations Active Record includes the majority of its validations from ActiveModel::Validations all of which accept the :on argument to define the context where the validations are act.

I’m not sure if this is exactly what you’re looking to do, but it  
might look something like:  
def validate\_on\_update  
errors.add(“User Type”, “has no admins”) if UserType.admins.count  
== 0  
end

_warning, not tested_

On Aug 13, 5:45 pm, Mark S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [August 14, 2008, 8:54am UTC](https://www.ruby-forum.com/t/validate-has-many-relationship-with-conditions/145792/3 "2008-08-14T08:54:41Z")

</div>

Thanks, I got pretty close with this suggestion…

turns out as I build this out more, i actually have users, groups, and  
roles (on these groups), with role\_types (1 is admin). in my roles  
controller, I have actions which add/remove roles from a given group  
(which correctly triggers my custom before\_destroy method). However, I  
can’t seem to find how to display the errors from “before\_destroy” upon  
the redirect back to the group page. So far, I’ve been using  
“error\_messages\_for :groups”, similar to on my forms. I’m guessing  
their is a problem displaying the errors generated by a “Role” in the  
“Group” pages. suggestions?

class User \< ActiveRecord::Base  
has\_many :roles  
has\_many :groups, :through =\> :roles  
end

class Group \< ActiveRecord::Base  
has\_many :roles  
has\_many :users, :through =\> :roles  
has\_many :owners, :through =\> :roles, :source =\> :user, :conditions =\>  
“role\_type\_id = 1”  
end

class RoleType \< ActiveRecord::Base  
has\_many :roles  
end

class Role \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :group  
belongs\_to :role\_type

def before\_destroy  
if (role\_type\_id.to\_i == 1 && Group.find(group\_id.to\_i).owners.count  
\<= 1)  
errors.add(:group, “Groups require at least one owner”)  
false  
end  
end  
end

class RolesController \< ApplicationController  
def remove (group\_id = params[:group\_id], role\_type\_id =  
params[:role\_type\_id], user\_id = @current\_user.id)  
conditions = {:group\_id =\> group\_id, :role\_type\_id =\> role\_type\_id,  
:user\_id =\> user\_id }  
Role.destroy\_all(conditions)  
redirect\_to :controller =\> :groups, :action =\> “show”, :id =\>  
group\_id  
end  
end

gsterndale wrote:

> You can overwrite validate\_on\_update in your User model.
> 
> [ActiveRecord::Validations](http://api.rubyonrails.org/classes/ActiveRecord/Validations.html#M001044)
> 
> I’m not sure if this is exactly what you’re looking to do, but it  
> might look something like:  
> def validate\_on\_update  
> errors.add(“User Type”, “has no admins”) if UserType.admins.count  
> == 0  
> end
> 
> _warning, not tested_
> 
> On Aug 13, 5:45 pm, Mark S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
