Forum: Ruby on Rails Clear errors on a given attribute

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

Regards,
Fyodor Golos
This topic is locked and can not be replied to.