我使用内置的csv函数require 'csv'
。但是当我尝试导出数据时,它只导出当前页面中的记录(包含10个条目)而不是全部。我应该改变什么?
我的sales.controller.rb:
def index
@sales = Sale.accessible_by(current_ability).order("created_at desc").paginate(:page => params[:page], :per_page => 10)
respond_to do |format|
format.html
format.csv { send_data @sales.to_csv, filename: "sales-#{Date.today}.csv"}
end
end
我的sale.rb型号:
def self.to_csv
attributes = %w{created_at id brand token customer_id orderid user_id product channel1 channel2}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |sale|
csv << sale.attributes.values_at(*attributes)
end
end
end
答案 0 :(得分:3)
如果您想获取所有记录而不是pagination
,那么
def index
@all_sales = Sale.accessible_by(current_ability).order("created_at desc")
@sales = @all_sales.paginate(:page => params[:page], :per_page => 10)
respond_to do |format|
format.html
format.csv { send_data @all_sales.to_csv, filename: "sales-#{Date.today}.csv"}
end
end
享受......!