この例では、フィールド検証(チームを選択する必要があります)が表示されます。そして、あなたはクラス/ベースレベルのバリデーションを見ることができます。たとえば、連絡先、電話や電子メールの少なくとも一つの方法を必要と:
class Registrant
include MongoMapper::Document
# Attributes ::::::::::::::::::::::::::::::::::::::::::::::::::::::
key :name, String, :required => true
key :email, String
key :phone, String
# Associations :::::::::::::::::::::::::::::::::::::::::::::::::::::
key :team_id, ObjectId
belongs_to :team
...
# Validations :::::::::::::::::::::::::::::::::::::::::::::::::::::
validate :validate_team_selection
validate :validate_contact_method
...
private
def validate_contact_method
# one or the other must be provided
if phone.empty? and email.empty?
errors.add_to_base("At least one form of contact must be entered: phone or email")
end
end
def validate_team_selection
if registration_setup.require_team_at_signup
if team_id.nil?
errors.add(:team, "must be selected")
end
end
end
end
日(Railsの3+)までのための第三の回答を参照してくださいソリューションの偉大な例については –