Notes posted by balint_erdi
RSS feedhave your to_param begin with the object's id
If you overwrite the to_param method in your model class such that it does not begin with its id, you can be in for a nasty surprise:
Example
class User def to_param self.login end ... end
Let’s say you have a user called “bob”, than you might think this works:
>> bob = User.find(3) => #<User id: 3, login: "bob", ...> >> User.find(bob.to_param) ActiveRecord::RecordNotFound: Couldn't find User with ID=bob
But it’s not the reason being that Rails find method looks for a beginning number (d+) and uses that to look up the record (and ignores everything that comes after the last digit). So the solution is to have your to_param return something that begins with the object’s id, like so:
Example
class User def to_param "#{self.id}-#{self.login}" end ... end >> bob = User.find(3) => #<User id: 3, login: "bob", ...>
>> User.find(bob.to_param)
> # id: 3, login: “bob”, …>
>> bob.to_param