title: Rails export csv including join table attributes
body: <p>I have 3 models in my app + associated join tables: </p>
  
  <p>Rating Set:</p>
  
  <pre><code>class RatingSet &lt; ActiveRecord::Base
    has_many :product_rating_sets, :dependent =&gt; :destroy
    has_many :products, :through =&gt; :product_rating_sets
  end
  </code></pre>
  
  <p>Product:</p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
    has_many :product_recommendations, :dependent =&gt; :destroy
    has_many :recommendations, :through =&gt; :product_recommendations
    has_many :product_rating_sets, :dependent =&gt; :destroy
    has_many :rating_sets, :through =&gt; :product_rating_sets  
  end
  </code></pre>
  
  <p>Recommendations:</p>
  
  <pre><code>class Recommendation &lt; ActiveRecord::Base
    has_many :product_recommendations, :dependent =&gt; :destroy
    has_many :products, :through =&gt; :product_recommendations
  end
  </code></pre>
  
  <p>I am trying to add an export to csv function. </p>
  
  <p>My method so far:</p>
  
  <pre><code>def self.as_csv
      CSV.generate do |csv|
        csv &lt;&lt; ["set_id", "time", "item_id", "related_item_id", "rating"]
        set_id = ['1','2','3']
        time = ['10:40am', "11:30pm", "14:40am"]
        item_id = ["123456","123456765", "123456543"]
        related_item_id = ["1234565432345","109876532", "564783920"]
        rating = ["12.3", "13.2", "132.2"]
  
        all.each do |item|
          csv &lt;&lt; item.attributes.values_at(*column_names)#lost here
        end
      end
    end
  </code></pre>
  
  <p>I am planning on adding scoped to find set_id, time, item_id, related_item_id and rating, but for testing, I am using the values above. How to I iterate through each of those arrays, and added the values to the exported csv file under their respective headers?</p>