remove old school validations and replaced with new validates method. Pending: fix active_record guide

This commit is contained in:
eparreno 2010-11-15 19:21:40 +01:00 committed by Xavier Noria
parent d5779efaf2
commit 3d377454db
5 changed files with 36 additions and 36 deletions

@ -96,7 +96,7 @@ To verify whether or not an object is valid, Rails uses the +valid?+ method. You
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
validates :name, :presence => true
end
Person.create(:name => "John Doe").valid? # => true
@ -109,7 +109,7 @@ Note that an object instantiated with +new+ will not report errors even if it's
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
validates :name, :presence => true
end
>> p = Person.new
@ -147,7 +147,7 @@ This method is only useful _after_ validations have been run, because it only in
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
validates :name, :presence => true
end
>> Person.new.errors[:name].any? # => false
@ -355,7 +355,7 @@ This helper validates that the specified attributes are not empty. It uses the +
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name, :login, :email
validates :name, :presence => true, :login, :email
end
</ruby>
@ -505,7 +505,7 @@ class Person < ActiveRecord::Base
validates_numericality_of :age, :on => :update
# the default (validates on both create and update)
validates_presence_of :name, :on => :save
validates :name, :presence => true, :on => :save
end
</ruby>
@ -603,7 +603,7 @@ Returns an OrderedHash with all errors. Each key is the attribute name and the v
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
validates :name, :presence => true
validates_length_of :name, :minimum => 3
end
@ -623,7 +623,7 @@ h4(#working_with_validation_errors-errors-2). +errors[]+
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
validates :name, :presence => true
validates_length_of :name, :minimum => 3
end
@ -699,7 +699,7 @@ The +clear+ method is used when you intentionally want to clear all the messages
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
validates :name, :presence => true
validates_length_of :name, :minimum => 3
end
@ -723,7 +723,7 @@ The +size+ method returns the total number of error messages for the object.
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
validates :name, :presence => true
validates_length_of :name, :minimum => 3
validates_presence_of :email
end

@ -672,11 +672,11 @@ Active Record validation error messages can also be translated easily. Active Re
This gives you quite powerful means to flexibly adjust your messages to your application's needs.
Consider a User model with a +validates_presence_of+ validation for the name attribute like this:
Consider a User model with a validation for the name attribute like this:
<ruby>
class User < ActiveRecord::Base
validates_presence_of :name
validates :name, :presence => true
end
</ruby>
@ -706,7 +706,7 @@ For example, you might have an Admin model inheriting from User:
<ruby>
class Admin < User
validates_presence_of :name
validates :name, :presence => true
end
</ruby>
@ -733,27 +733,27 @@ So, for example, instead of the default error message +"can not be blank"+ you c
* +count+, where available, can be used for pluralization if present:
|_. validation |_.with option |_.message |_.interpolation|
| validates_confirmation_of | - | :confirmation | -|
| validates_acceptance_of | - | :accepted | -|
| validates_presence_of | - | :blank | -|
| validates_length_of | :within, :in | :too_short | count|
| validates_length_of | :within, :in | :too_long | count|
| validates_length_of | :is | :wrong_length | count|
| validates_length_of | :minimum | :too_short | count|
| validates_length_of | :maximum | :too_long | count|
| validates_uniqueness_of | - | :taken | -|
| validates_format_of | - | :invalid | -|
| validates_inclusion_of | - | :inclusion | -|
| validates_exclusion_of | - | :exclusion | -|
| validates_associated | - | :invalid | -|
| validates_numericality_of | - | :not_a_number | -|
| validates_numericality_of | :greater_than | :greater_than | count|
| validates_numericality_of | :greater_than_or_equal_to | :greater_than_or_equal_to | count|
| validates_numericality_of | :equal_to | :equal_to | count|
| validates_numericality_of | :less_than | :less_than | count|
| validates_numericality_of | :less_than_or_equal_to | :less_than_or_equal_to | count|
| validates_numericality_of | :odd | :odd | -|
| validates_numericality_of | :even | :even | -|
| confirmation | - | :confirmation | -|
| acceptance | - | :accepted | -|
| presence | - | :blank | -|
| length | :within, :in | :too_short | count|
| length | :within, :in | :too_long | count|
| length | :is | :wrong_length | count|
| length | :minimum | :too_short | count|
| length | :maximum | :too_long | count|
| uniqueness | - | :taken | -|
| format | - | :invalid | -|
| inclusion | - | :inclusion | -|
| exclusion | - | :exclusion | -|
| associated | - | :invalid | -|
| numericality | - | :not_a_number | -|
| numericality | :greater_than | :greater_than | count|
| numericality | :greater_than_or_equal_to | :greater_than_or_equal_to | count|
| numericality | :equal_to | :equal_to | count|
| numericality | :less_than | :less_than | count|
| numericality | :less_than_or_equal_to | :less_than_or_equal_to | count|
| numericality | :odd | :odd | -|
| numericality | :even | :even | -|
h5. Translations for the Active Record +error_messages_for+ Helper

@ -584,7 +584,7 @@ h3. Active Record and Referential Integrity
The Active Record way claims that intelligence belongs in your models, not in the database. As such, features such as triggers or foreign key constraints, which push some of that intelligence back into the database, are not heavily used.
Validations such as +validates_uniqueness_of+ are one way in which models can enforce data integrity. The +:dependent+ option on associations allows models to automatically destroy child objects when the parent is destroyed. Like anything which operates at the application level these cannot guarantee referential integrity and so some people augment them with foreign key constraints.
Validations such as +validates :foreign_key, :uniqueness => true+ are one way in which models can enforce data integrity. The +:dependent+ option on associations allows models to automatically destroy child objects when the parent is destroyed. Like anything which operates at the application level these cannot guarantee referential integrity and so some people augment them with foreign key constraints.
Although Active Record does not provide any tools for working directly with such features, the +execute+ method can be used to execute arbitrary SQL. There are also a number of plugins such as "foreign_key_migrations":http://github.com/harukizaemon/redhillonrails/tree/master/foreign_key_migrations/ which add foreign key support to Active Record (including support for dumping foreign keys in +db/schema.rb+).

@ -550,7 +550,7 @@ Ruby uses a slightly different approach than many other languages to match the e
<ruby>
class File < ActiveRecord::Base
validates_format_of :name, :with => /^[\w\.\-\+]+$/
validates :name, format => /^[\w\.\-\+]+$/
end
</ruby>

@ -315,7 +315,7 @@ Now to get this test to pass we can add a model level validation for the _title_
<ruby>
class Post < ActiveRecord::Base
validates_presence_of :title
validates :title, :presence => true
end
</ruby>