I have a User model witch i have override the to_param method.
i made it like this:
class User < ActiveRecord::Base
def to_param
if self.id < 100
if self.id < 10
s = "00#{self.id}"
else
s = "0#{self.id}"
end
else
s = "#{self.id}"
end
return "usr-#{s}"
end
end
And on my controller:
user = User.find(params[:id])
@title = user.name
the problem is… i can`t get any users…
if i change the to_param method to: return "#{s}-usr" it simple ignores the -usr and search for the id without taking -usr
i already tried an after_save filter, but i could`n change an atribute on there.
how can i create an unique id to an user based on his id ?
Rails won’t do inverse transformation for you, you have to do it yourself. I think you can do it like that:
and then use it in wherever you need it:
In addition, have a look at
to_paramdocs. Description and examples clearly state that this method is used for generating urls, but developer is the one responsible for finding corresponding record by this parameter.Regarding strings formatting you can find more info in docs for
String#%method andKernel#formatmethod it uses.