As you can see at http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_associated
This method has been deprecated.
I want to know the 开发者_StackOverflow中文版correct method that can be used in its place to validate associated models.
Thanks Neelesh
You could create your own method and callback.
class ShoppingCart < ActiveRecord::Base
has_many :items
before_validation :check_items
def check_items
items.each do |item|
# check if valid and add to errors if any
end
return false if errors.any? # this will cause save to return false
end
end
See http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
validates_associated is not deprecated. Do not trust apidock.com - it shows deprecation warnings for many things due to internal changes in Rails 2 and 3. See http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_associated
精彩评论