0
我在我的應用程序3款+相關的連接表:Rails的導出CSV包括連接表的屬性
類別設定:
class RatingSet < ActiveRecord::Base
has_many :product_rating_sets, :dependent => :destroy
has_many :products, :through => :product_rating_sets
end
產品:
class Product < ActiveRecord::Base
has_many :product_recommendations, :dependent => :destroy
has_many :recommendations, :through => :product_recommendations
has_many :product_rating_sets, :dependent => :destroy
has_many :rating_sets, :through => :product_rating_sets
end
建議:
class Recommendation < ActiveRecord::Base
has_many :product_recommendations, :dependent => :destroy
has_many :products, :through => :product_recommendations
end
我正在嘗試添加一個導出到csv函數。
我的方法至今:
def self.as_csv
CSV.generate do |csv|
csv << ["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 << item.attributes.values_at(*column_names)#lost here
end
end
end
我就準備加入作用域找到SET_ID,時間,ITEM_ID,related_item_id和評級,但測試中,我使用上述值。如何遍歷每個數組,然後將這些值添加到導出的csv文件的各自標題下?