Prev | Current Page 214 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

If
you place it on both sides, you will create a dreaded infinite loop! Consider this example
(do not do this!):
class Account < ActiveRecord::Base
has_many :accountrights
validates_associated :accountrights
end
class Accountright < ActiveRecord::Base
belongs_to :account
validates_associated :account
end
??? The validates_associated method only ensures that the association is valid; it does not
check that the association is present. To do that, you should also include the validates_
presence_of method, as in this example:
class Account < ActiveRecord::Base
has_many :accountrights
end
class Accountright < ActiveRecord::Base
belongs_to :account
validates_associated :account
validates_presence_of :account, :on => :update
end
Validates_associated also accepts the :on and :if options explained in previous validation
methods.
validates_numericality_of
Use validates_numericality_of to ensure that the value of the associated attribute is numeric.
The following example ensures that the submitted value of account_age can be converted with
Kernal.


Pages:
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226