Validate uniqueness of many to many association in Rails

后端 未结 2 612
星月不相逢
星月不相逢 2021-02-20 00:51

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.

H

2条回答
  •  [愿得一人]
    2021-02-20 01:21

    Try 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
    

提交回复
热议问题