编写 Rails 数组查询
我需要编写一个验证,以确保在我们的应用程序中创建的新记录没有重复的名称。我被告知 validates_uniqueness 在这种情况下不起作用,因为应用程序已经使用它来验证原始名称的创建。此验证将确保当记录用新名称更新时,它也是原始的......这是一种反范围验证。该查询还需要不区分大小写,因此我需要使用 MYSQL 的 UPPER() 函数来编写它,这就是为什么我需要使用数组查询而不是哈希。
这是伪代码的草图。
查询 CUSTOMER 数据库。 检查以确保 UPDATED_RECORD 的名称是否唯一。 如果它不是唯一的,请检查它的 ID 是否属于正在更新的记录。 如果是,那没关系,因为它是相同的 ID,并且应该允许保留相同的名称。 如果查询发现具有与其关联的不同 ID 的相同名称,则应显示一条错误消息,例如“此名称已被 XXXX 使用”。
我对 Rails 相当陌生,并且花了很长时间才正确地构建这个结构。
这里有一些关于为什么我认为 validates_uniqueness 在这里不起作用的澄清。
我认为问题在于同一记录有多个版本,只要更新的记录是同一记录的新版本(它具有相同的记录 ID),那么这就是有效的。但是,如果使用现有记录已使用的名称(具有不同的记录 ID)更新新记录版本,则不应允许这种情况。例如,有两个术语版本记录,{:record_id => 100, :name => "Test"} & {:record_id => 100, :name => "Test"},因为这两个记录是同一术语的版本, {:record_id => 100, :name => "Test"} & {:record_id => 201, :name => "Test"},因为这会导致两个版本共享名称相同但与不同的术语相关联。
I need to write a validation to make sure that new records created in our application do not have duplicate names. I'm told that validates_uniqueness will not work in this instance because the application already uses it to validate the creation of the original name. This validation will ensure that when the record is updated with a new name, that it too, is original...it's sort of an anti-scope validation. The query also needs to be case insensitive so I will need to write it using MYSQL's UPPER() function, which is why I'll need to use an array query and not a hash.
Here's a sketch of the pseudo code.
Query the CUSTOMER database.
Check to ensure the UPDATED_RECORD to see if its NAME is unique.
If it's NOT unique check to see if it's ID belongs to record that's being updated.
If it is, that's OK as it's the same ID and should be allowed to keep the same name.
If the query finds the same name that has a different ID associated with it, then an error message should be displayed such as "This Name is already in use by XXXX."
I'm fairly new to Rails and have been having a bear of a time getting this structured correctly.
Here's some clarification on why I don't think validates_uniqueness will work here.
I think the issue is having multiple versions of the same record, as long as updated record is a new version of the same record (it has the same record ID), then that's valid. But if a new record version is updated with a name that's already used by an existing record (with a different record ID, then that shouldn't be allowed. For example, having two term version records, {:record_id => 100, :name => "Test"} & {:record_id => 100, :name => "Test"}, should be allowed since those two records are versions of the same term. But having two records, {:record_id => 100, :name => "Test"} & {:record_id => 201, :name => "Test"}, should not be allowed since that would result in two versions sharing the same name but associated with different terms.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白为什么你不能使用 validates_uniqueness。您说它已经用于验证原始名称的创建,我猜测该列与您现在要检查的列不同,但这并不重要。
当您创建新记录或更新现有记录时,这些验证将通过。您还可以通过在迁移中添加索引来设置唯一性验证。这提供了额外的安全性。
编辑
关于您的澄清,添加自定义验证可能会更容易,如下所示:
I don't understand why you can't use validates_uniqueness. You said it's already used to validate the creation of the original name, which I am guessing is a column different than the one you want to now check, but that doesn't matter.
When you create a new record, or update an existing one, these validations will pass. You can also set a uniqueness validation in the migration by adding an index on it. This gives extra security.
EDIT
Regarding your clarification, it might be easier to add a custom validation, as follows: