title: Ruby send method with rails associations
body: <p>I have been messing about with making a sortable table module thing.  I know some might exist but want to get experience doing this myself.  I had the idea of have it like so:</p>
  
  <pre><code>SortedTable.new(ModelName, Hash_Of_Columns_And_Fields, ID)
  </code></pre>
  
  <p>example</p>
  
  <pre><code>SortedTable.new(Post, {"Title" =&gt; "title", "Body" =&gt; "body", "Last Comment" =&gt; "comment.last.title"}, params[:id])
  </code></pre>
  
  <p>I am planning to do something like:</p>
  
  <pre><code>def initialize(model, fields, id)
    data = {}
    model = model.capitalize.constantize
    model.find(id)
    fields.each do |column, field|
      data[column] = model.send(field)
    end
  end
  </code></pre>
  
  <p>This works fine for title and body but when it comes to getting the <code>Last Comment</code> with <code>comment.last.title</code> it errors out.  I have tried doing <code>Post.send("comments.last.title")</code> but says <code>NoMethodError: undefined method 'comments.last.title' for #&lt;Post:0x0000010331d220&gt;</code></p>
  
  <p>I know I can do <code>Post.send("comments").send("last").send("title")</code> and that works but I can not think of how to do that dynamically by taking the fields and spliting the on . then chaining the sends.  Can anyone give me advice on how to do this?  If I am doing this completely wrong also then please say or point me in the direction of code that does something similar.  I am not a expert ruby developer but I am trying.</p>
  
  <p>P.S The above code might not work as I am not at a computer with ruby/rails to test, but hopefully you get the concept.</p>
  
  <p>Cheers</p>