Forum: Ruby Cleare errors on a given attribute

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Fyodor G. (Guest)
on 2006-12-29 19:05
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

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 :)

Fyodor Golos
Fyodor G. (Guest)
on 2006-12-29 19:09
My bad. Wrong list. Reposted in "Ruby on Rails"
This topic is locked and can not be replied to.