Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.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嵌套的\u属性\u进行验证_Ruby On Rails_Validation_Nested Forms - Fatal编程技术网

Ruby on rails 接受\u嵌套的\u属性\u进行验证

Ruby on rails 接受\u嵌套的\u属性\u进行验证,ruby-on-rails,validation,nested-forms,Ruby On Rails,Validation,Nested Forms,我使用的是Rails 2.3.8,并且接受的是嵌套的属性 我有一个简单的category对象,它使用awesome_nested_set来允许嵌套的类别 对于每个类别,我想要一个名为code的唯一字段。这对于每个级别的类别来说是唯一的。这意味着父类别都将具有唯一的代码,子类别在其自己的父类别中将是唯一的 例如: 这在没有验证过程的情况下有效,但当我尝试使用以下内容时: 验证以下项的唯一性::code,:scope=>:parent\u id 这将不起作用,因为父对象尚未保存 这是我的模型: cl

我使用的是Rails 2.3.8,并且接受的是嵌套的属性

我有一个简单的category对象,它使用awesome_nested_set来允许嵌套的类别

对于每个类别,我想要一个名为code的唯一字段。这对于每个级别的类别来说是唯一的。这意味着父类别都将具有唯一的代码,子类别在其自己的父类别中将是唯一的

例如:

这在没有验证过程的情况下有效,但当我尝试使用以下内容时: 验证以下项的唯一性::code,:scope=>:parent\u id

这将不起作用,因为父对象尚未保存

这是我的模型:

class Category < ActiveRecord::Base
  acts_as_nested_set
  accepts_nested_attributes_for :children, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true

  default_scope :order => "lft"

  validates_presence_of :code, :name, :is_child
  validates_uniqueness_of :code, :scope => :parent_id
end
类别lambda{a | a[:name].blank?}、:allow_destroy=>true的_嵌套_属性
默认范围:order=>“lft”
验证是否存在:code、:name、:is\u子项
验证以下项的唯一性::code,:scope=>:parent\u id
结束
我已经想到了一种不同的方法来实现这一点,它非常接近于工作,问题是我无法检查子类别之间的唯一性

在第二个示例中,我在名为“is_child”的表单中嵌入了一个隐藏字段,以标记该项是否为子类别。以下是我对该模型的示例:

class Category < ActiveRecord::Base
  acts_as_nested_set
  accepts_nested_attributes_for :children, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true

  default_scope :order => "lft"

  validates_presence_of :code, :name, :is_child
  #validates_uniqueness_of :code, :scope => :parent_id
  validate :has_unique_code

  attr_accessor :is_child


  private 
  def has_unique_code

    if self.is_child == "1"
      # Check here if the code has already been taken this will only work for 
      # updating existing children.
    else

      # Check code relating to other parents
      result = Category.find_by_code(self.code, :conditions => { :parent_id => nil})

      if result.nil?
        true
      else
        errors.add("code", "Duplicate found")
        false
      end
    end
  end
end
类别lambda{a | a[:name].blank?}、:allow_destroy=>true的_嵌套_属性
默认范围:order=>“lft”
验证是否存在:code、:name、:is\u子项
#验证以下项的唯一性::code,:scope=>:parent\u id
验证:是否具有唯一的\u代码
属性访问器:是否为子级
私有的
def具有唯一的代码
如果self.is_child==“1”
#检查这里,如果代码已经被接受,这将只适用于
#更新现有子项。
其他的
#检查与其他家长相关的代码
result=Category.find_by_code(self.code,:conditions=>{:parent_id=>nil})
如果结果为0.0?
真的
其他的
错误。添加(“代码”,“找到重复项”)
假的
结束
结束
结束
结束
这非常接近。如果有一种方法可以在accepts_nested_attributes_下的reject_If语法中检测重复代码,那么我就会在那里。这一切似乎过于复杂,但我想建议,使它更容易。我们希望继续在一个表单中添加类别和子类别,以加快数据输入

更新: 也许我应该在保存之前使用build或。而不是

validates_uniqueness_of :code, :scope => :parent_id
试一试

除此之外,您还需要在Category类中设置:

has_many :children, :inverse_of => :category # or whatever name the relation is called in Child

使用的逆函数将使子变量
parent
在保存之前进行设置,并且它有可能工作。

谢谢!作为验证的一部分,我一直在尝试使用其父对象中的值验证嵌套字段。由于父对象不可用,因此在新对象上失败。声明的反作用使它起作用了!
validates_uniqueness_of :code, :scope => :parent
has_many :children, :inverse_of => :category # or whatever name the relation is called in Child