Cleare errors on a given attribute


#1

I’ve been studying Rails source code to figure out how to clear
ActiveRecord errors for a given attribute name. It appears that there is
no direct API to do that. The best workaround I found is this:

active_record.errors.add(attribute_name, “dummy”).clear

Basically, “add” returns an array of error messages for given attribute
name, which I subsequently clear. Seems to work, but doesn’t feel
“right”.

In case anyone wonders why in the world I needed such functionality,
here is a brief explanation. I have an object with a has_many
association. If any of the children fail validation, the following error
is displayed for the parent object: “(insert association name here) is
invalid”. I do not find this particular error message to be very
informative and instead would rather display exact children errors.
Hence, I would like to make a sweep over parent object errors and remove
any errors on associations after validation but before rendering.

If anyone has any better suggestions on better displaying children
errors, I am all ears :slight_smile:

Regards,
Fyodor Golos


#2

My bad. Wrong list. Reposted in “Ruby on Rails”