Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Feb 24 ’11

Rails 3 Validation Shortcut

In Rails 2 the only way to add multiple validations to a field is through separate validate statements:

validates_presence_of :title
validates_length_of :title, :maximum => 30

Rails 3 simplifies this process by adding a method called validates which is a “shortcut to all default validators”. Using the validates method your code will look like this:

validates(:title, :presence => true, :length => {:maximum => 30})

You can find more information about this method in the API documentation here.

12 notes 0 comments

  1. cfurrow reblogged this from rubyquicktips
  2. atm09td reblogged this from rubyquicktips
  3. rubyflare reblogged this from rubyquicktips
  4. Dushyanth submitted this to rubyquicktips

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus