我的模型将作者彼此联系起来.我想验证作者不能自己交朋友.我知道我可以在UI级别处理这个问题,但是我希望能够进行验证以防止UI中的错误允许它.我试过validates_exclusion_of,但它不起作用.这是我的关系模型:
class FrIEndship < ActiveRecord::Base # prevent duplicates valIDates_uniqueness_of :frIEnd_ID,:scope => :author_ID # prevent someone from following themselves (doesn't work) valIDates_exclusion_of :frIEnd_ID,:in => [:author_ID] attr_accessible :author_ID,:frIEnd_ID belongs_to :author belongs_to :frIEnd,:class_name => "Author"end解决方法 您必须使用自定义验证:
class FrIEndship < ActiveRecord::Base # ... valIDate :disallow_self_referential_frIEndship def disallow_self_referential_frIEndship if frIEnd_ID == author_ID errors.add(:frIEnd_ID,'cannot refer back to the author') end endend总结
以上是内存溢出为你收集整理的ruby-on-rails – 验证自引用关联不会链接回rails中的原始实例全部内容,希望文章能够帮你解决ruby-on-rails – 验证自引用关联不会链接回rails中的原始实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)