Coding Hell

Programming and stuff.

Hide Actual Database IDs From Your Users

In a project I’m working on in my spare time, I wanted to hide / obfuscate the databases IDs of my ActiveRecord model. I didn’t want the users to find records by simply incrementing a number in the URL.

I ended up writing my own Gem, which manipulates ActiveRecord::Base to hide the actual ID. Rails’s path functions still work as expected. The only thing I had to add to the actual project was the following line:

1
2
3
class User < ActiveRecord::Base
  encrypted_id key: '5gA6lgr5g3GOg7EOQ1caYQ'
end

I open sourced the project on GitHub (pencil/encrypted_id) and published it on RubyGems.org.

Comments