开发者

Alternate method for deprecated "validates_associated" method in ruby in rails

开发者 https://www.devze.com 2023-02-17 00:50 出处:网络
As you can see at http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_associated This method has been deprecated.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消