Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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 在RubyonRails中保存相关表属性之前,如何在验证中访问这些属性_Ruby On Rails_Validation_Nested Attributes - Fatal编程技术网

Ruby on rails 在RubyonRails中保存相关表属性之前,如何在验证中访问这些属性

Ruby on rails 在RubyonRails中保存相关表属性之前,如何在验证中访问这些属性,ruby-on-rails,validation,nested-attributes,Ruby On Rails,Validation,Nested Attributes,如果某个属性设置为false,我希望能够跳过验证,比如status,问题是这个模型有很多嵌套属性,如果status设置为false,它们也需要跳过验证 这种植入的目的是,如果出于任何原因想要保存表单条目的草稿,让其通过验证可能会阻止其保存,这是不可取的行为,只需要通过将状态设置为true来验证即将公开的条目 下面的例子 位于项目中的属性状态 model Article has_many :sub_articles accepts_nested_attributes_for :sub_ar

如果某个属性设置为false,我希望能够跳过验证,比如status,问题是这个模型有很多嵌套属性,如果status设置为false,它们也需要跳过验证

这种植入的目的是,如果出于任何原因想要保存表单条目的草稿,让其通过验证可能会阻止其保存,这是不可取的行为,只需要通过将状态设置为true来验证即将公开的条目

下面的例子

位于项目中的属性状态

model Article
  has_many :sub_articles
  accepts_nested_attributes_for :sub_articles, :allow_destroy => true

  validate_presence_of :body, :unless => Proc.new{|article| !article.status }

model SubArticle
  belongs_to :article
  has_many :foos
  accepts_nested_attributes_for :foos, :allow_destroy => true    

  validate_presence_of :body, :unless => Proc.new{|sub_article| !sub_article.article.status }

model Foo
  belongs_to :sub_article

  validate_presence_of :body, :unless => Proc.new{|foo| !foo.sub_article.article.status }
跳过对文章的验证将起作用,但对子文章或Foo则不起作用,因为它们尚未保存,并且没有设置ID,无法像示例中那样遍历关联

是否可以在验证点访问关联的表属性

任何帮助都将不胜感激

------更新------

子节和Foo验证失败的原因是

未定义的方法“状态” 零:零级

这是预期的,因为验证的时间,文章id或子文章id仍然为零,我已经通过这样做确认了这一点

model SubArticle
  belongs_to :article
  has_many :foos

  validate_presence_of :body, :unless => Proc.new{|sub_article| !sub_article.article.check_attributes }

def check_attributes
  pp self
end
女巫给予

#<SubArticle id: nil, body: "", article_id: nil, created_at: nil, updated_at: nil >
#
-----更新2----------


我忘记了重要的细节,模型的所有数据都是使用嵌套表单一次输入的,因此在创建时,所有模型上的所有条目都只在内存中,修复了示例代码,以便更为明显。

这是可能的,但您需要使用
=>
而不是
=
。像这样:

validate_presence_of :body, :unless => Proc.new{|article| !article.status }

有关条件验证的更多信息,请参阅:了解。

如何对ID进行条件验证

model Article
  has_many :sub_articles

  validate_presence_of :body, :unless => Proc.new{|article| !article.status }

    model SubArticle
      belongs_to :article
      has_many :foos

      validate_presence_of :body, :unless => Proc.new{|sub_article| !sub_article.article_id}

model Foo
  belongs_to :sub_article

  validate_presence_of :body, :unless => Proc.new{|foo| !foo.sub_article_id }

不确定这是否可行,但实际上只有在父模型已保存并因此具有ID的情况下才运行验证。但对于更新不起作用。也许这段代码和您的原始代码的混合可以用于更新?

我已经让它工作了,尽管有点黑客味,但通过使用:的逆\u解决了无法遍历关联的问题,因为ID为零

model Article
  has_many :sub_articles, :inverse_of => :article

  validate_presence_of :body, :unless => Proc.new{|article| !article.status }

model SubArticle
  belongs_to :article, :inverse_of => :sub_articles
  has_many :foos, :inverse_of => :foo

  validate_presence_of :body, :unless => Proc.new{|sub_article| !sub_article.article.status }

model Foo
  belongs_to :sub_article, :inverse_of => :foos

  validate_presence_of :body, :unless => Proc.new{|foo| !foo.sub_article.article.status }

这将使所有层中正在检查的实例都可以访问status属性。

对不起,那些键入错误的地方,修复了它们。它可以很好地处理文章模型,但不能处理子文章或Foo。这真的很奇怪,它非常适合我。您使用的是什么rails版本?使用rails 3.0.7和ruby 1.8.7,更新的问题,代码在更新时可以工作,但在创建时会失败。但这是否意味着无论创建时的status属性如何,都将跳过验证?有可能创建时状态为真。介意发布一个关于如何使用模型的快速示例吗?