Using Rails 3, I have an account model which has_many addresses. In the address model I have a before_destroy callback method defined which prevents the destroy and adds an error to the address object if the address is associated with a third model. This works fine to prevent the delete of the account or the address.
My issue comes when deleting the account as I would expect the account object's errors to contain the address error but it does not. Is there any way to include the address error message within the account errors?
Error messages are generally assigned to the model object where they apply.
If you really want to, you could grab those errors and stick them in your address model's errors hash:
class Account < ActiveRecord::Base
before_destroy :check_for_destruction
def check_for_destruction
rejected = addresses.reject{|a| a.can_destroy?} # returns array of addresses that now have errors (they should return false)
rejected.each do |address|
address.errors.each do |e|
errors.add_to_base(e)
end
end
end
end
Something like that should work, assuming you have defined an Address#can_destroy?
method.
(Disclaimer: code is untested, but should give you a good jumping off point)