Alex Rivera | Logout

Validate uniqueness of many to many association in Rails

Asked 2009-09-14T13:28:49.997
12

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.

Edit
Report

1 Answer

16

I think what you want is validates_uniqueness_of:

class Taggings
  belongs_to :tags
  validates_uniqueness_of :tag_id, :scope => :project_id
end

This is what I'm using, and works well.

answered 2010-07-02T23:16:49.423

Your Answer