Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails “已弃用”的替代方法;验证“U关联”;方法在ruby-in-rails中的应用_Ruby On Rails_Validation - Fatal编程技术网

Ruby on rails “已弃用”的替代方法;验证“U关联”;方法在ruby-in-rails中的应用

Ruby on rails “已弃用”的替代方法;验证“U关联”;方法在ruby-in-rails中的应用,ruby-on-rails,validation,Ruby On Rails,Validation,正如你在 此方法已被弃用 我想知道可以用来验证相关模型的正确方法 谢谢 Neelesh您可以创建自己的方法和回调 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

正如你在

此方法已被弃用

我想知道可以用来验证相关模型的正确方法

谢谢
Neelesh

您可以创建自己的方法和回调

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
class ShoppingCart

请参见验证未弃用的相关\u。不要信任apidock.com-它显示了由于Rails2和Rails3中的内部更改而导致的许多内容的弃用警告。请参见

假设您使用的是ActiveRecord,这是一种有效的方法。我想要“验证”的普通方式。在此之前,我将使用validates_associated。
validates_associated
在ActiveModel 4.0中消失。问题是关于
ActiveModel
,而不是
ActiveRecord