Birds-Eye.Net
All things broadband and more...
 
Web Birds-Eye.Net

What's New?

Ruby on Rails (RoR)
Programming Reference


Models
External database connections
Passing current_user into model
Passing object into model
Using static lookup tables
Validates IF
Validates MongoMapper

Views
Dynamically delete form element
Edit create nested data
HTML form field check_box
Layout jQuery datatable module
Select array
Select cascading via JS
Text_area Array
Text_area listing submit
Text field format time

Controllers
Dynamic model selection
Including first item from a sorted desc table
Using from_unixtime on epoch dates

Rack
Integrated NTLM/Kerberos Authentication
Pass-through Authentication w/ NTLM

ActionMailer
Broken links in emails

Rails General
Add, Subtract, Multiply, and Divide
Calculate number of weekdays for date range
Date->Epoch & Epoch->Date
Extract first letter of each word
Hash of hashes assignment
Using: variable as hash index

jQuery
jQuery accordion MongoDB


More to come

 

Use of Model Validations with MongoMapper

By: Bruce Bahlmann - Contributing Author (your feedback is important to us!)

In rails development, when working with MongoDB, you often have a particular field dependent on another or want to verify that all fields are filled out correctly prior to committing these changes to the database. Fortunately, MongoMapper provides support for this through the use of a custom validation.

Custom validations are contained within the model they pertain and referenced with the call to validate:<name of custom validation function>

[app/models/survey.rb]
class Survey 
  include MongoMapper::Document

  key :name, String
  key :description, String 
  key :author, Hash
  <other model definitions> ...
  
  validate :custom_validation
   
  def custom_validation
    errors.add(:name, "- Survey Name cannot be blank, it is required.") if (name == '')
    errors.add(:endDate, '- End Date has not been selected.') if (active == true) && string_to_date(endDate).nil?
    if !string_to_date(endDate).nil?
      errors.add(:endDate, '- End Date cannot be in the past, it must be sometime in the future.') if (endDate <= DateTime.now)
    end  
  end   
end   

The above entry provides three example validations. The first represents a simple check of the name field and return an error if the name field is blank (name == ''). Note that in this case, the ":name" within the brackets defines the name of the field in error so when such data is returned (in error) to the user, the user will know better what might have went wrong.

The second validation is a little more complicated. It involves a check for the existence of a date (endDate). Since endDate could be nil, you need to handle these fields with more care (test for nil first, then test for a valid value). Once the nil case is off the table (tested/accommodated), moving forward testing other potentially harmful values then becomes easier.

Can Birds-Eye.Net help you or your Company?
Receive your Birds-Eye.Net articles and white papers hot off the presses by adding our RSS feed to your reader.

 

 

(C) Copyright Birds-Eye.Net, All rights reserved.
It is against the law to reproduce this content or any portion of it in any form without the explicit written permission of Birds-Eye Network Services, LLC. Federal copyright law (17 USC 504) makes it illegal, punishable with fines up to $100,000 per violation plus attorney's fees.