简体   繁体   English

Rails 唯一性:true model 带有带有 has_many 引用的数据库 - Ruby on Rails

[英]Rails Uniqueness: true model with a DB with has_many references - Ruby on Rails

I have a problem.我有个问题。 I need to set a model of report_templates unique in my database in relation to the classroom , each classroom has only one report_template .我需要在我的数据库中设置一个 model 与classroom相关的唯一report_templates ,每个classroom只有一个report_template But in my DB, I already have a classroom with multiple report_templates .但是在我的数据库中,我已经有一个包含多个report_templates的教室。

is it safe to add this validation to report_template model upload to staging?将此验证添加到report_template model 上传到暂存是否安全?

There are two approaches:-有两种方法:-

  1. If you add validation then you will get error when you are editing existing report templates.如果添加验证,那么在编辑现有报告模板时会出现错误。 This is because validations are called when you save the record.这是因为在您保存记录时会调用验证。 So you might have to remove the duplicate/multiple records manually via console or db or a rake task.因此,您可能必须通过控制台或数据库或 rake 任务手动删除重复/多条记录。 Simply accessing the record will not throw an error.简单地访问记录不会抛出错误。

  2. If one classroom has only report template do you think it is better to use either of below?如果一个教室只有报告模板,您认为使用以下哪个更好? I do not know the exact scenario so cannot suggest an approach.我不知道确切的情况,所以不能建议一种方法。

  • has_one association有一个协会
  • belongs_to association所属协会

Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM