How would I stop the last user being deleted. The following code doesn''t work. before_destroy :dont_destroy_last_user # Don''t delete user if it the last one def dont_destroy_last_User raise "Can''t destroy last user" if User.length < 1 end -- Posted via http://www.ruby-forum.com/.
On 18-feb-2006, at 18:30, Alexander wrote:> How would I stop the last user being deleted. > The following code doesn''t work.before_destroy :check_if_last_user private def check_if_last_user raise "Last user can''t be deleted" if self.class.count == 1 end
Alexander wrote:> How would I stop the last user being deleted. > The following code doesn''t work. > > > before_destroy :dont_destroy_last_user > > # Don''t delete user if it the last one > def dont_destroy_last_User > raise "Can''t destroy last user" if User.length < 1 > endChange to:> # Don''t delete user if it the last one > def dont_destroy_last_User > raise "Can''t destroy last user" if User.count == 1 > endUntested,but should work joey__ -- Posted via http://www.ruby-forum.com/.
Julian ''Julik'' Tarkhanov wrote:> On 18-feb-2006, at 18:30, Alexander wrote: > >> How would I stop the last user being deleted. >> The following code doesn''t work. > > before_destroy :check_if_last_user > > private > def check_if_last_user > raise "Last user can''t be deleted" if self.class.count == 1 > endThanks Also ''raise'' creates quite a ugly error message. Is there any way of redirecting and using flash instead? Alex -- Posted via http://www.ruby-forum.com/.
On 18-feb-2006, at 19:06, Alexander wrote:> Julian ''Julik'' Tarkhanov wrote: >> On 18-feb-2006, at 18:30, Alexander wrote: >> >>> How would I stop the last user being deleted. >>> The following code doesn''t work. >> >> before_destroy :check_if_last_user >> >> private >> def check_if_last_user >> raise "Last user can''t be deleted" if self.class.count == 1 >> end > > Thanks > > Also ''raise'' creates quite a ugly error message. Is there any way of > redirecting and using flash instead?I think the neat way would be with an exception catcher in the model: class LastUserCantBeDeleted < RuntimeError; end; .... raise LastUserCantBeDeleted if self.class.count == 1 in the controller begin @user.destroy! rescue LastUserCantBeDeleted flash[:error] = "This is the last one'' redirect_to :referer # or something like that end A less neat way would be to return false from the method and thus stop the callback chain, and then unless @user.destroy! flash[:error] = "This is the last one" # ... etc end
Julian ''Julik'' Tarkhanov wrote:> class LastUserCantBeDeleted < RuntimeError; end; > > .... raise LastUserCantBeDeleted if self.class.count == 1 > > > in the controller > > begin > @user.destroy! > rescue LastUserCantBeDeleted > flash[:error] = "This is the last one'' > redirect_to :referer # or something like that > end > > A less neat way would be to return false from the method and thus stop > the callback chain, and then > > > unless @user.destroy! > flash[:error] = "This is the last one" > # ... etc > endThis will not work if you have two independent FCGI/SCGI processes trying to delete the last user at the same time. Might sound bit extreme, but its quite possible for example if the user clicks rapidly the submit button twice. You''ll need to wrap the deletion and the counting of users in a transaction. The first beta release of AWDWR had similar code for preventing the deletion of the last administrator. Revised version was simpler not allowing to delete ''Dave'' user. That''s another alternative. -- Company - http://primalgrasp.com Thoughts - http://deezsombor.blogspot.com