0

This is probably a huge oversight on my part, but I have been searching for a fix for about an hour now, with no luck!

I am following the RailsCast tutorial on exporting data to .CSV

The data I am using to populate the CSV is coming from a Ransack query (in the params)

However, the data that is being spit out is a collection of the data objects, not the raw data itself --

Output in the Browser:

#<Order:0x007f820823c738>,#<Order:0x007f82082ad708>,#<Order:0x007f82082ace98>,
#<Order:0x007f82082ac718>

And I am just trying to get those same objects, but just printed out... like in the RailsCasts tutorial (Screenshot)

Here is my Orders_Controller

def index
  @search = Order.search(params[:q])
  @orders = @search.result

  @results = @orders

  respond_to do |format|
    format.html # index.html.erb
    format.csv { render text: @results.to_csv }
  end
end

So is there an easy way to convert those data objects into a raw string like in the tutorial?

EDIT

Here is my to_csv method in my model

def self.to_csv(options = {})
  CSV.generate(options) do |csv|
    csv << column_names
    all.each do |order|
      csv << order.attributes.values_at(*column_names)
    end
  end
end
briankulp
  • 186
  • 1
  • 11

1 Answers1

2

So I think this is the problem: @search.result is an array of Orders, so @results.to_csv is calling the Array#to_csv method. Since you're not getting an ActiveRecord::Relation you probably need to convert to csv differently.

Create a ransack_helpers.rb module in lib:

module RansackHelpers
  def self.to_csv(ransack_result, options = {})
    CSV.generate(options) do |csv|
      result_array = ransack_result.result
      column_names = result_array.first.class.column_names
      csv << column_names
      result_array.each do |item|
        csv << item.attributes.values_at(*column_names)
      end
    end
  end
end

In orders_controller.rb:

def index
  @search = Order.search(params[:q])
  @orders = @search.result

  respond_to do |format|
    format.html # index.html.erb
    format.csv { render text: RansackHelpers.to_csv(@search) }
  end
end

I haven't tested this code, so there might be some issues with it, but I think this is what you want to do.

Raphael
  • 1,701
  • 15
  • 26
  • Is this helper file going in `/lib/assets/`? or `/lib/tasks`? or just in the `helpers` directory? – briankulp Sep 06 '12 at 22:05
  • Just put them in the `/lib`, or if you want, you can organize them into subdirectories off of `/lib`. ref: http://stackoverflow.com/questions/4906932/how-to-create-and-use-a-module-using-ruby-on-rails-3 – Raphael Sep 07 '12 at 14:46
  • Okay, so it seems to be referencing the helper file now properly, however when I include the `RansackHelpers` in my controller, it seems to ignore the query params in the URL and return no records (even though there are records within the search params). When I remove the `include RansackHelpers` in the controller, the query results work properly but I then get a `uninitialized constant OrdersController::RansackHelpers` when I hit the .CSV formatted route. Any ideas? Thanks so much for your help thus far BTW. I really appreciate it! – briankulp Sep 07 '12 at 18:25
  • Make sure your code matches my example. Don't include the module. If it still doesn't work, please add your full controller definition. – Raphael Sep 07 '12 at 21:05
  • Well, for some reason, I couldn't get it to work properly, so my solution was to render it into a .csv.erb view and manually call each attribute in CSV format. I had a pretty tight deadline to hit for this feature, but I will hopefully explore this solution further for a future update. – briankulp Sep 10 '12 at 16:20
  • Could you paste in the code you tried (controller and module)? – Raphael Sep 11 '12 at 15:59