加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 验证Rails中多对多关联的唯一性

发布时间:2020-12-16 22:57:02 所属栏目:百科 来源:网络整理
导读:说我有项目,这是与Tag的多对多关联.我使用has_many,所以我有单独的连接模型. 如何创建验证,检查连接模型的唯一性?现在我只有 has_many :tags,:through = :taggings,:uniq = true 但这并不能保存. 解决方法 尝试 validates_associated. 相信允许连接模型验证
说我有项目,这是与Tag的多对多关联.我使用has_many,所以我有单独的连接模型.

如何创建验证,检查连接模型的唯一性?现在我只有

has_many :tags,:through => :taggings,:uniq => true

但这并不能保存.

解决方法

尝试 validates_associated.

相信允许连接模型验证在保存之前运行.所以在你的情况下:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读