I have a database in which email is set as unique validation with status. I have set scope as a int 'status' which is 1 when particular account is active. So if user switches his country, his account status is switched to 0 and a new account is created with same email and status is set as 1. So to solve this I have used a composite unique key on 'email' and 'status' as,
validates :email, uniqueness: { scope: :status }
So, let's have a small dry run. currently, user is in X country and his email is [email protected] so database may look like
email: [email protected]
status: 1
Now if he goes to country Y database updates as,
[for company X]
email: [email protected]
status: 0
[for company Y]
email:[email protected]
status: 1
works fine right but now if our Traveler goes to Country Z now database will stuck as now for company Y status will update to 0 and as now this combination is not unique so will return error. Also the challenging task here to only use email and status as the keys. What I want to achieve is that for a particular email status:1 must be unique while status:0 can be any number of times. So I was trying to condition the statement like
validates :email, uniqueness: { scope: :status }, if: :status == 1
but no luck as I cannot take status value of particular user in model. Thanks, in advance!! PS: I am new to ROR, so do provide relevant links so I can learn more xD.