Reputation: 617
this is my model User
class User < ApplicationRecord
extend FriendlyId
friendly_id :name
end
the user name is not uniq.
i want my url like this:
http://localhost:3000/users/myname
this is my controller:
class UsersController < ApplicationController
before_action :set_user
def set_user
@user = User.friendly.find(params[:id])
end
end
the URL is shown fine, just the way i want, but the problem is when i try to delete a user, the console server shows a "rollback" i notice that this is because there are two or more users with the same name so It can delete the user. So my problem now that I have implemented gem friendly_id is that it searchs and set_user by name but i want to set_admin by ID so i can delete by id (this way it does not matter if there are many users with the same name). This problem is repeated in my other models too.
How can avoid this behavoir, I need to set model by id but to show the name or encrypt the id in the url. Do you recomend me other gem or how should i implement correctly. (is not an option to set name as uniq). thanks
Upvotes: 0
Views: 130
Reputation: 106
As per my understanding, friendly id uses uniqueness validation at db level
If you already have users with same name, you should delete those entries. (This is much similar to condition where we try to put unique index to a column in db where same entry values already exist)
Then install friendly_id gem, and make entries. I am sure slugs created by gem wont repeat so every record will be unique and you will be able to delete record using id Too
Upvotes: 0