title: Can I use AR object as hash key or should I use object_id instead
body: <p>Because of Ruby awesomeness it is possible to use any object as key</p>
  <pre><code>document = Document.find 1
  o = Hash.new
  o[1] = true
  o[:coool] = 'it is'
  o[document] = true
  # an it works
  
  o[document]
  #=&gt; true
  </code></pre>
  <p>but just because it is possible doesn't mean is good practice</p>
  <p>However I have situation where in my controller I need to set something similar, so I can loop trough it in view</p>
  <pre><code>#controller
  @users_with_things = Hash.new
  Things.accessible_by(some_curent_user_logic).each do |thing|
    @user_with_things[thing.user] ||= Array.new
    @user_with_things[thing.user] &lt;&lt;  thing.id
  end 
  
  #view
  - @users_with_things.each do |user, thing_ids|
    %input{type: :checkbox, name: &quot;blank[user_#{user.id}]&quot;, value: 1, class: &quot;select_groups&quot;, :'data-resource-ids' =&gt; &quot;[#{thing_ids.join(',')}]&quot;, :'data-user-type' =&gt; user.type }
  </code></pre>
  <p>The reason why I want to do it this way is because I don't want to call from my view <code>User.find_by_id</code>  (want to make it clean)</p>
  <pre><code>#controller
  @users_with_things = Hash.new
  Things.accessible_by(some_curent_user_logic).each do |thing|
    @user_with_things[thing.user.id] ||= Array.new
    @user_with_things[thing.user.id] &lt;&lt;  thing.id
  end 
  
  #view
  - @users_with_things.each do |user_id, thing_ids|
    - user = User.find user_id
    %input{type: :checkbox, name: &quot;blank[user_#{user.id}]&quot;, value: 1, class: &quot;select_groups&quot;, :'data-resource-ids' =&gt; &quot;[#{thing_ids.join(',')}]&quot;, :'data-user-type' =&gt; user.type }
  </code></pre>
  <p>So my 1st question is: <strong>is it ok to use ActiveRecord object as Hash key in situation like this</strong></p>
  <p>I can imagine several scenarios where this may go wrong (sessions, when object changes in model and so on) however this is just for rendering in a view</p>
  <h2>Alternative !</h2>
  <p>so this is one way to do it, the other may be like this</p>
  <pre><code>#controller
  @users_with_things = Hash.new
  Things.accessible_by(some_curent_user_logic).each do |thing|
    @user_with_things[thing.user.object_id] ||= Array.new
    @user_with_things[thing.user.object_id] &lt;&lt;  thing.id
  end 
  
  #view
  - @users_with_things.each do |user_object_id, thing_ids|
    - user = ObjectSpace._id2ref(user_object_id)  #this will find user object from object_id
    %input{type: :checkbox, name: &quot;blank[user_#{user.id}]&quot;, value: 1, class: &quot;select_groups&quot;, :'data-resource-ids' =&gt; &quot;[#{thing_ids.join(',')}]&quot;&quot;, :'data-user-type' =&gt; user.type }    
  </code></pre>
  <p>...which is even more, hardcore. However it is way around if for some reason <code>hash[ARobject] = :something</code> would create big memory cluster for some reason</p>
  <p>question 2 : <strong>is it good idea to do it this way ?</strong></p>
  <hr />
  <p>to be complete there is also another alternative and that is</p>
  <pre><code># ...
  @user_with_thing[ [thing.user.id, thing.user.type] ] &lt;&lt; thing_id
  # ...
  </code></pre>
  <p>so basically array object will be key</p>
  <pre><code>@user_with_thing[ [1, 'Admin'] ] 
  #=&gt; [1,2,3]
  </code></pre>