行ではなく列ごとに csv ファイルにデータを追加するにはどうすればよいですか。
私が現在持っているもの:
def self.as_csv(rating_id)
CSV.generate do |csv|
csv << ["set_id","item_id", "aggregated_rating", "related_item_id", "rating", "time"]
product_ids = RatingSet.find(rating_id).products
product_ids.each do |product|
recommendation_ids =Recommendation.find(:all, :joins => :products, :conditions => ["product_id = ? and rating_set = ?", product.id, rating_id])
recommendation_ids.each do |recommendation|
Rating.find(:all, :conditions => ["recommendation_id = ? and rating_set = ? and product_id = ?", recommendation.id, rating_id, product.id]).each do |rating|
time_updated = (rating.updated_at)
csv << [rating_id, product.id, product.rating, recommendation.id, rating.rating, time_updated]
end
end
end
end
end
これは基本的に、データのセットごとに新しい行を生成します。例:
102 4433175 2.4 10391793 1 2013-03-26 18:33:40 UTC
102 4433175 2.4 10391794 1 2013-03-26 18:33:40 UTC
102 4433175 2.4 12526899 1 2013-03-26 18:33:40 UTC
102 4433175 2.4 19235377 1 2013-03-26 18:33:40 UTC
行ではなく新しい列を追加して生成するにはどうすればよいですか:
102 4433175 2.4 10391793 1 10391794 1 12526899 1 19235377 1 2013-03-26 18:33:41 UTC