aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/active_record_validations.md
diff options
context:
space:
mode:
Diffstat (limited to 'guides/source/active_record_validations.md')
-rw-r--r--guides/source/active_record_validations.md28
1 files changed, 18 insertions, 10 deletions
diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md
index c1b0059cb1..541b1a1e84 100644
--- a/guides/source/active_record_validations.md
+++ b/guides/source/active_record_validations.md
@@ -4,14 +4,14 @@ Active Record Validations
This guide teaches you how to validate the state of objects before they go into
the database using Active Record's validations feature.
-After reading this guide and trying out the presented concepts, we hope that you'll be able to:
+After reading this guide, you will know:
-* Understand the life cycle of Active Record objects
-* Use the built-in Active Record validation helpers
-* Create your own custom validation methods
-* Work with the error messages generated by the validation process
-* Create callback methods that respond to events in the object life cycle
-* Create special classes that encapsulate common behavior for your callbacks
+* Understand the life cycle of Active Record objects.
+* Use the built-in Active Record validation helpers.
+* Create your own custom validation methods.
+* Work with the error messages generated by the validation process.
+* Create callback methods that respond to events in the object life cycle.
+* Create special classes that encapsulate common behavior for your callbacks.
--------------------------------------------------------------------------------
@@ -365,12 +365,20 @@ class Person < ActiveRecord::Base
end
```
-If you want to be sure that an association is present, you'll need to test whether the foreign key used to map the association is present, and not the associated object itself.
+If you want to be sure that an association is present, you'll need to test the associated object itself, and not whether the foreign key used to map the association is present:
```ruby
class LineItem < ActiveRecord::Base
belongs_to :order
- validates :order_id, presence: true
+ validates :order, presence: true
+end
+```
+
+You should also be sure to have a proper `:inverse_of` as well:
+
+```ruby
+class Order < ActiveRecord::Base
+ has_many :line_items, inverse_of: :order
end
```
@@ -667,7 +675,7 @@ class Invoice < ActiveRecord::Base
:discount_cannot_be_greater_than_total_value
def expiration_date_cannot_be_in_the_past
- if !expiration_date.blank? and expiration_date < Date.today
+ if expiration_date.present? && expiration_date < Date.today
errors.add(:expiration_date, "can't be in the past")
end
end