How do I avoid having duplicate rows in a has_many :through table?
I have a Center model and a Department model. The linked :through the Local model. However, I want to avoid inserting the same match twice in the Local table.
Do I use a validate statement or there is a different way of telling Rails to raise an error? What would be the best way?
I got it. I used :
validates_uniqueness_of :center_id, scope: :department_id
Yep, exactly! You can also add an index on that table that includes those two columns in order to speed up the query this validation executes to check validity. That'd be the only other thing you'd probably want to add.