You can't add errors before_destroy because they won't impact the validation. You have to add the errors before the validation occurs, with e. G before_validation There are some additional callbacks before_validation_on_create and before_validation_on_update There is no on_destroy version, though, because save and valid?
Is never called, so adding validation errors when you try to destroy something would be pointless In other words: your before_destroy runs, but it doesn't affect anything, because nothing checks for validation errors when you destroy records Here's one take on implementing something that prevents record saving based on certain conditions before_destroy :require_no_book_loans private def require_no_book_loans raise ActiveRecord::RecordInvalid unless book_loans. Count == 0 end You could also create your own error class and raise that FYI: You are supposed to pass a block to before_destroy in your syntax error example before_destroy {|r| r.errors. Add_to_base "Foo" # ... }.
You can't add errors before_destroy, because they won't impact the validation. You have to add the errors before the validation occurs, with e.g. Before_validation. There are some additional callbacks, before_validation_on_create and before_validation_on_update.
There is no on_destroy version, though, because save and valid? Is never called, so adding validation errors when you try to destroy something would be pointless.In other words: your before_destroy runs, but it doesn't affect anything, because nothing checks for validation errors when you destroy records. Here's one take on implementing something that prevents record saving based on certain conditions.
Before_destroy :require_no_book_loans private def require_no_book_loans raise ActiveRecord::RecordInvalid unless book_loans. Count == 0 end You could also create your own error class and raise that. FYI: You are supposed to pass a block to before_destroy in your syntax error example.
Before_destroy {|r| r.errors. Add_to_base "Foo" # ... }.
– ahsteele Sep 25 '09 at 19:26 I'll update the answer, sec :) – August Lilleaas Sep 25 '09 at 19:28 Sweet so this will raise an error. Considering that exceptions should only occur in exceptional circumstances would I be better off preventing the destroy link from even showing up when the user has loans? – ahsteele Sep 25 '09 at 19:35 1 Yeah, that sounds sensible.
People could still forge it by making a request to destroy it by hand, but you're raising an error so it won't be destroyed anyway. – August Lilleaas Sep 25 '09 at 19:39 Cool thanks for the help. – ahsteele Sep 25 '09 at 19:41.
You can't add errors before_destroy, because they won't impact the validation. You have to add the errors before the validation occurs, with e.g. There are some additional callbacks, before_validation_on_create and before_validation_on_update. There is no on_destroy version, though, because save and valid?
Is never called, so adding validation errors when you try to destroy something would be pointless. In other words: your before_destroy runs, but it doesn't affect anything, because nothing checks for validation errors when you destroy records. Here's one take on implementing something that prevents record saving based on certain conditions.
You could also create your own error class and raise that. FYI: You are supposed to pass a block to before_destroy in your syntax error example.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.