Note that there are some explanatory texts on larger screens.

plurals
  1. POObfuscating ids in Rails app
    primarykey
    data
    text
    <p>I'm trying to obfuscate all the ids that leave the server, i.e., ids appearing in URLs and in the HTML output.</p> <p>I've written a simple Base62 lib that has the methods encode and decode. Defining—or better—overwriting the id method of an ActiveRecord to return the encoded version of the id and adjusting the controller to load the resource with the decoded <code>params[:id]</code> gives me the desired result. The ids now are base62 encoded in the urls and the response displays the correct resource.</p> <p>Now I started to notice that subresources defined through <code>has_many</code> relationships aren't loading. e.g. I have a record called <code>User</code> that <code>has_many</code> <code>Post</code>s. Now <code>User.find(1).posts</code> is empty although there are posts with <code>user_id = 1</code>. My explanation is that ActiveRecord must be comparing the <code>user_id</code> of <code>Post</code> with the <strong>method</strong> <code>id</code> of <code>User</code>—which I've overwritten—instead of comparing with <code>self[:id]</code>. So basically this renders my approach useless.</p> <p>What I would like to have is something like defining <code>obfuscates_id</code> in the model and that the rest would be taken care of, i.e., doing all the encoding/decoding at the appropriate locations and preventing ids to be returned by the server.</p> <p>Is there any gem available or does somebody have a hint how to accomplish this? I bet I'm not the first trying this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload