Ignore empty nested attributes when using Rails Admin

How to resolve issues that arise with saving models if you have empty nested attributes using reject_if

When using Rails Admin we use accepts_nested_attributes_for to give our clients a cleaner and less complicated view. They were recently having issues with saving an instance of a model with a field which was not required. On a little more digging we discovered that they created an empty instance of a nested rails model.

To resolve this we changed our accepts_nested_attributes_for to use reject_if:

accepts_nested_attributes_for :cover_image, allow_destroy: true, reject_if: proc { |attribute| attribute[:body].blank? }

This checks to see if the body attribute (which is required in the nested model) is blank and if so rejects it meaning that the parent model can save.

Recent posts View all


Writing accessible hashtags

Write accessible hashtags by camel casing them. Because ThisReadsBetter thanthisdoes.


Database Consistency Gem

The database consistency gem was new to me but it is fast becoming a must-install tool