How to Convert Array of Activerecord Models to CSV

How to convert array of ActiveRecord models to CSV?

The following will write the attributes of all users to a file:

CSV.open("path/to/file.csv", "wb") do |csv|
csv << User.attribute_names
User.find_each do |user|
csv << user.attributes.values
end
end

Similarly you could create a CSV string:

csv_string = CSV.generate do |csv|
csv << User.attribute_names
User.find_each do |user|
csv << user.attributes.values
end
end

How to convert an array of objects to CSV in Rails

It looks like it's an array of Hashes. To access properties of a hash in Ruby you need to use brackets. Try updating your code to this:

csv << attributes.map{ |attr| obj.send([], attr) }

or more concisely:

csv << attributes.map{ |attr| obj[attr] }

One more thing, in the example you provided, the keys in the hash are symbols which means you may need to convert your attributes to symbols when trying to access them, like this:

csv << attributes.map{ |attr| obj[attr.to_sym] }

convert array to CSV file on ruby

I managed to do this with(2nd edit) proper indentation

headers = ["user_id", "created_at"]
CSV.open("myfile.csv", "w", :col_sep => "\t| ", :headers => true) do |csv|
csv << headers
csv << ["-"*(headers.join.length+4*headers.length)] #Header separator in the length of header columns + tabs
a.each {|row| csv << row } #Adding rows for each element of a
end

Create CSV file with data from my model

The following code will write the attributes of all users to a file:

CSV.open("path/to/file.csv", "wb") do |csv|
csv << User.attribute_names
User.all.each do |user|
csv << user.attributes.values
end
end

Source: How to convert array of ActiveRecord models to CSV? (duplicate question)

Output array to CSV in Ruby

To a file:

require 'csv'
CSV.open("myfile.csv", "w") do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ...
end

To a string:

require 'csv'
csv_string = CSV.generate do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ...
end

Here's the current documentation on CSV: http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html

Exporting CSV in Rails with a class method being called on an array of objects

My question should have been "Why would sending the message, to_csv, to an instance of ActiveRecord::Relation cause a method on the Product class object to be invoked?"

In Rails, class methods automatically become available as "collection" methods; This is the reason why they are available to Relation objects (See this SO question/answer I missed earlier: how does this Ruby class method get invoked?).

How to convert Class name ActiveRecord::Result to ActiveRecord::Relation

So I finally figure it out. I don't know If its the right way though so I have this query using ransack.

@payment_report = Spree::Order.ransack(
orders_gt: params[:q][:created_at_gt],
orders_lt: params[:q][:created_at_lt],
payments_order_id_in: [Spree::Order.ids],
payments_state_eq: 'completed',
orders_id_in: [Spree::LineItem.all.pluck(:order_id)],
variants_id_in: [Spree::LineItem.ids]
).result.includes(:payments, :line_items, :variants).joins(:line_items, :payments, :variants)

apparently, you can do multiple queries on ransack but it wasn't there on their documentation I found the answer on bug report on their GitHub. yea so for now I have this. but the raw query is different from here. so still have a problem

Separating an Array into a comma separated string with quotes

This functionality is built into ActiveRecord:

Model.where(:my_field => ['blue','green','red'])


Related Topics



Leave a reply



Submit