在构建情况下验证所属关联

发布于 2024-08-27 03:31:24 字数 559 浏览 8 评论 0原文

我有一个具有多个任务的任务模型,并且该任务属于任务

为了安全起见,我对任务模型进行了此验证:

  validates_presence_of :mission_id
  validates_numericality_of :mission_id

但问题是,当创建任务并添加这样的任务时:

 @mission.tasks.build

验证返回错误,因为任务任务的 id 为空(任务尚未创建)

如果我删除验证,任务和任务将成功创建,但如何保留验证并仍然进行这项工作? 我可以在保存后进行回调,但我认为这是不对的,因为我不想在没有 Mission_id 的情况下保存任务。

聚苯乙烯 我在表格上隐藏了我的任务字段。如果我让它可见,它将显示当前的任务并且一切正常。但如果我隐藏它,就会发生错误。

<%=  f.hidden_field :mission, :label => "Missão" %>

表单是否会重置控制器在新操作上给出的属性?

I have a Mission model that has_many Task, and the Task belongs_to Mission

For security I've made this validation on the Task Model:

  validates_presence_of :mission_id
  validates_numericality_of :mission_id

But the problem is that when create a Mission and add tasks like this:

 @mission.tasks.build

The validation returns error, because the mission id on the task is null ( the mission wasn't yet created )

If I delete the validation, the Mission and Task is created successfuly, but how can I keep the validation and still have this work?
I could do a callback after the save, but I don't think that's right, because I don't want to save Tasks without a mission_id.

P.S.
I'm hidding my mission field on the form. If I have it visible, it will show the currect mission and everything is ok. But if I hidde it the error happens.

<%=  f.hidden_field :mission, :label => "Missão" %>

Is the form reseting the attributes given by the controller on the new action?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场信仰旅途 2024-09-03 03:31:24

验证嵌套属性时,您应该执行以下验证:

 validates_associated   :mission

When validating a nested attribute, you should do the following validation :

 validates_associated   :mission
吃→可爱长大的 2024-09-03 03:31:24

中使用以下内容

has_many :tasks
validates_associated :task

参考中的 Mission.rb Task.rb

belongs_to :mission

在控制器

@mission=Mission.new(params[:mission])  
task= @mission.tasks.build(params[:task]) ###this is same as Task.new(:mission_id=>@mission.id)

if @mission.save #this will save only when mission as well as task are valids, also it will automatically assign mission_id to tasks table you have nothing to worry about it<br>
else
    #your error code will be here.......
end

:- http:// api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

Use following in Mission.rb

has_many :tasks
validates_associated :task

Task.rb

belongs_to :mission

in controller

@mission=Mission.new(params[:mission])  
task= @mission.tasks.build(params[:task]) ###this is same as Task.new(:mission_id=>@mission.id)

if @mission.save #this will save only when mission as well as task are valids, also it will automatically assign mission_id to tasks table you have nothing to worry about it<br>
else
    #your error code will be here.......
end

Ref:- http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文