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
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