← Back to Upcase

Index on multiple columns and also make them unique


(Charlieanna) #1

Hi
I have a User which can join many challenges like he can join Walking Challenge, or a Diet Challenge. So Challenge has a name. There is another table called UserActivity that keeps a track on the user activity for that challenge
UserActivity belongs_to :user and :challenge. Should I put a unique index on user_id and challenge_id together because I want to make sure he cannot join the same challenge twice. Because challenge can only be joined through a team.


(Charlieanna) #2

Is this a better way to do it? It works though.

 it { should validate_uniqueness_of(:user_id).scoped_to(:challenge_id) }