简体   繁体   English

rails 2.3.8 validates_uniqueness_of with:scope 不工作

[英]rails 2.3.8 validates_uniqueness_of with :scope not working

I'm using rails 2.3.8 and I have a model with two attributes I want to validate by.我正在使用 rails 2.3.8 并且我有一个 model 有两个我想验证的属性。

I have version and file number and I need to validate uniqueness of the file number on update but only according to each individual unique version.我有版本和文件号,我需要在更新时验证文件号的唯一性,但仅根据每个单独的唯一版本。

So I've tried所以我试过了

 validates_uniqueness_of :file_number, :scope => :version, :on => :update

I repeatedly get "File number has already been taken"我反复收到“文件号已被占用”

There is no special logic with the file number except for in a before_create where I add one to the count of items for that version.文件编号没有特殊逻辑,除了在 before_create 中,我在该版本的项目计数中添加了一个。

EDIT: Here's the SQL that the server performs.编辑:这是服务器执行的 SQL。 http://pastie.org/private/ysbwtiyouhgqkjsjjbooa The id here is the id of the record I'm updating and the one returned from the query is another record. http://pastie.org/private/ysbwtiyouhgqkjsjjbooa这里的id是我正在更新的记录的id,查询返回的是另一条记录。

Any ideas?有任何想法吗?

EDIT- I got it: SOLUTION:编辑-我明白了:解决方案:

Turns out the validation wasn't looking across a select list of mymodel records which would be defined by结果证明验证没有查看 mymodel 记录的 select 列表,该列表将由

mymodel belongs to project and project has_many mymodel mymodel 属于项目和项目 has_many mymodel

so with the following tweak it worked.所以通过以下调整它起作用了。

validates_uniqueness_of:file_number, :scope => [:version, :project_id], :on =>:update validates_uniqueness_of:file_number, :scope => [:version, :project_id], :on =>:update

Pasting solution as answer:粘贴解决方案作为答案:

Turns out the validation wasn't looking across a select list of mymodel records which would be defined by结果证明验证没有查看 mymodel 记录的 select 列表,该列表将由

mymodel belongs to project and project has_many mymodel mymodel 属于项目和项目 has_many mymodel

so with the following tweak it worked.所以通过以下调整它起作用了。

validates_uniqueness_of:file_number, :scope => [:version, :project_id], :on =>:updat validates_uniqueness_of:file_number, :scope => [:version, :project_id], :on =>:updat

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

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