验证许多的独特性在Rails的一对多关联 [英] Validate uniqueness of many to many association in Rails
本文介绍了验证许多的独特性在Rails的一对多关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有项目,那就是在许多-to-many关联是标签即可。我使用的是*的has_many通过*所以我就单独的连接模式。
Say I have Project, that is in many-to-many association with Tag. I'm using *has_many through* so I have separate join model.
如何创建验证,来检查的独特加盟模式?现在,我只有
How do I create validation, that checks uniqueness of join model? Now I have only
has_many :tags, :through => :taggings, :uniq => true
但是,这并不确认保存。
But that doesn't validate on save.
推荐答案
尝试的 validates_associated 。
这是应该的,我相信,允许加入模型验证,在保存之前运行。所以,你的情况:
That should, I believe, allow the join model validations to run before saving. So in your case:
class Project
has many :tags, :through => :taggings
validates_associated :taggings
end
class Taggings
belongs_to :tags
#your validations here....
end
class Tag
has_many :taggings
end
这篇关于验证许多的独特性在Rails的一对多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文