title: Using Delegate With has_many In Rails?
body: <p>We've got 2 models &amp; a join model:</p>
  
  <pre><code>#app/models/message.rb
  Class Message &lt; ActiveRecord::Base
      has_many :image_messages
      has_many :images, through: :image_messages
  end
  
  #app/models/image.rb
  Class Image &lt; ActiveRecord::Base
      has_many :image_messages
      has_many :messages, through: :image_messages
  end
  
  #app/models/image_message.rb
  Class ImageMessage &lt; ActiveRecord::Base
      belongs_to :image
      belongs_to :message
  end
  </code></pre>
  
  <hr>
  
  <p><strong>Extra Attributes</strong></p>
  
  <p>We're looking to extract the extra attributes from the join model (<code>ImageMessage</code>) and have them accessible in the <code>Message</code> model:</p>
  
  <pre><code>@message.image_messages.first.caption # -&gt; what happens now    
  @message.images.first.caption #-&gt; we want
  </code></pre>
  
  <p>We've already achieved this using the <code>select</code> method when declaring the association:</p>
  
  <pre><code>#app/models/message.rb
  has_many :images, -&gt; { select("#{Image.table_name}.*", "#{ImageMessage.table_name}.caption AS caption") }, class_name: 'Image', through: :image_messages, dependent: :destroy
  </code></pre>
  
  <hr>
  
  <p><strong>Delegate</strong></p>
  
  <p>We've just found the <a href="http://apidock.com/rails/Module/delegate" rel="nofollow"><code>delegate</code></a> method, which does exactly what this needs. However, it only seems to work for <code>has_one</code> and <code>belongs_to</code> associations</p>
  
  <p>We just got this working with a single association, but it seems it does not work for collections (just takes you to a public method)</p>
  
  <hr>
  
  <p><strong>Question</strong></p>
  
  <p>Do you know any way we could return the <code>.caption</code> attribute from the <code>ImageMessage</code> join model through the <code>Image</code> model?</p>
  
  <p>We have this currently:</p>
  
  <pre><code>#app/models/image.rb
  Class Message &lt; ActiveRecord::Base
      has_many :image_messages
      has_many :messages, through: :image_messages
  
      delegate :caption, to: :image_messages, allow_nil: true
  end
  
  #app/models/image_message.rb
  Class ImageMessage &lt; ActiveRecord::Base
      belongs_to :image
      belongs_to :message
  
      def self.caption # -&gt; only works with class method
          #what do we put here?
      end
  end
  </code></pre>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks to Billy Chan (for the instance method idea), we have got it working <em>very</em> tentatively:</p>
  
  <pre><code>#app/models/image.rb
  Class Image &lt; ActiveRecord::Base
      #Caption
      def caption
          self.image_messages.to_a
      end
  end
  
  #app/views/messages/show.html.erb
  &lt;%= @message.images.each_with_index do |i, index| %&gt;
      &lt;%= i.caption[index][:caption] %&gt; #-&gt; works, but super sketchy
  &lt;% end %&gt;
  </code></pre>
  
  <p>Any way to refactor, specifically to get it so that each time <code>.caption</code> is called, it returns the <code>image_message.caption</code> value for that particular record? </p>