I have a HABTM association between user and role.
User can be an admin (role_id = 1) or a user (role_id = 2) for roles.
In the join table, roles_users, I have some redundant records. For ex:

I want to remove the duplicate records such as 1:1, 2:4.
Two questions:
-
Where’s the best place to execute the sql script that removes the dups — migration? script?
-
What is the sql query to remove the dups?
and for the future, to prevent duplicate rows
Or, you can do all of it in one step with
ALTER TABLE IGNORE:ALTER IGNORE TABLE roles_users ADD UNIQUE INDEX (role_id, user_id);