为什么ActiveRecord会自动验证has

系统教程 行业动态 更新时间:2024-06-14 17:03:54
为什么ActiveRecord会自动验证has_many关联(Why ActiveRecord automatically validates has_many association)

以下模型给出:

class Question < ActiveRecord::Base has_many :answers end class Answers < ActiveRecord::Base belongs_to: question validates :comment, presence: true end

打电话时

question = Question.new question.answers.build question.valid?

valid? 返回false因为相关答案无效。 写作时

has_many :answers, validate: false

Question valid? 返回true 。

这是一个错误还是在使用has_many时需要自动验证相关模型? Rails指南明确解释了validate_associated与has_many关系的使用: http : //guides.rubyonrails.org/active_record_validations_callbacks.html#validates_associated

Following models are given:

class Question < ActiveRecord::Base has_many :answers end class Answers < ActiveRecord::Base belongs_to: question validates :comment, presence: true end

When calling

question = Question.new question.answers.build question.valid?

valid? returns false because the associated answer is not valid. When writing

has_many :answers, validate: false

in Question valid?returns true.

Is it a bug or is it desired when using has_many the associated models are validated automatically? The Rails Guides explicitly explain the use of validate_associated with a has_many relationship: http://guides.rubyonrails.org/active_record_validations_callbacks.html#validates_associated

最满意答案

这绝对不是一个bug。

你已经创建了一个Question 您已经告诉Rails附加一个新的Answer实例并将其与这个新Question联系起来 然后你问“Rails,我创建的Question/Answer模型和关联是否已准备好保存到数据库中?”

正如您所发现的,Rails会在您的案例中说“不”

我从未使用过,也不关心validates_associated 。 但是,我可以向您指出文档,解释您为什么会看到自己的行为。

Active Record Autosave Association

虽然上面的源文件中的文档值得一读,但我会为你提取这些内容

请注意:autosave => false与不声明不同:autosave。 如果:自动保存选项不存在,则会保存新关联。

你没有在你的:answers关联中指定:autosave => SOMETHING 因此,Rails默认情况下会尝试在新Question上保存新构建/关联的Answer 保存将失败,因为Answer无效

It's most definitely not a bug.

You've created a Question You've told Rails to attach a new instance of Answer and relate it to this new Question You're then asking "Rails, is this Question/Answer model and association I've created ready to be saved to the database?"

As you've found, Rails will say "No" in your case.

I have never used and do not care about validates_associated. I can however point you to documentation explaining why you're seeing the behavior you are.

Active Record Autosave Association

Though the documentation at the above source file is worth reading in it's entirety, I'll pull out this bit for you

Note that :autosave => false is not same as not declaring :autosave. When the :autosave option is not present new associations are saved.

You have not specified :autosave => SOMETHING on your :answers association Because of this, Rails by default is going to try and save your newly build/associated Answer on your new Question The save will fail because the Answer is invalid

更多推荐

本文发布于:2023-04-24 14:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/02b90ee94a5ff30392482a39987aec29.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ActiveRecord

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!