rails delete_all exception
Posted
by Sergei
on Stack Overflow
See other posts from Stack Overflow
or by Sergei
Published on 2010-04-26T19:47:41Z
Indexed on
2010/04/27
22:43 UTC
Read the original article
Hit count: 261
Hello, guys!
I have some User model, Role model and Assignment model to manage role-based user system.
Just like here .
So, here some kind of code, to include roles into users form:
<% for role in Role.all %>
<%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %>
<%=h role.name %><br />
<% end %>
And all's working fine for new records and for editing records.
But I just want to make possible to restrict deleting Assigment with id=1, cause this Assignment for default root user from migration, who must be admin anyway.
Otherwise, I can lose my last admin.
So I've tryed to add before_destroy to Assignment.rb model, but it doesn't work.
I've noticed, that when I'm editing user and trying to remove his admin role(assignment), the server makes a delete_all method, not destroy method.
So, does anybody know how to catch the deleting of Assignment with id=1 through this kind of code?
Thanks in advance!
© Stack Overflow or respective owner