我想导出数据到CSV文件。我这个代码导轨4导出为CSV:未定义的方法`属性'
@products = Product.all
respond_to do |format|
format.csv { render text: @products.to_csv }
end
,并在我的模型
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv << column_names
all.each do |product|
csv << self.attributes.values_at(*column_names)
end
end
end
这是行不通的。我得到这个错误
NoMethodError in ProductsController#index
undefined method `attributes' for #<Class:0x000000047e91c8>
我在Rails 4和红宝石2.2。我怎样才能解决这个问题?
尝试'product.attributes.values_at(* column_names)'而不是'self.attributes.values_at(* column_names)' – Pavan