Rails: how to require at least one field not to be blank
问题 I know I can require a field by adding validates_presence_of :field to the model. However, how do I require at least one field to be mandatory, while not requiring any particular field? thanks in advance -- Deb 回答1: You can use: validate :any_present? def any_present? if %w(field1 field2 field3).all?{|attr| self[attr].blank?} errors.add :base, "Error message" end end EDIT: updated from original answer for Rails 3+ as per comment. But you have to provide field names manually. You could get all