David Kimura PRO said over 6 years ago on Importing and Exporting CSV Data :

Have a look at this branch compare. (https://github.com/driftingrub...

You basically will need to temporarily create a file to store it and pass the path to ActiveJob. While this is currently working on local filestore, it can also work with S3 or fog.


David Kimura PRO said over 6 years ago on Importing and Exporting CSV Data :

The * is a splat operator. So, take the following example.

[2] pry(main)> fields
=> ["first_name", "last_name"]
[3] pry(main)> User.first.attributes.values_at(fields)
User Load (0.5ms)SELECT`users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
=> [nil]
[4] pry(main)> User.first.attributes.values_at(*fields)
User Load (0.5ms)SELECT`users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
=> ["System", "Administrator"]

http://blog.honeybadger.io/rub...

The ! at the end of find_or_create_by will raise an exception on validation errors.


osazemeu said about 6 years ago on Importing and Exporting CSV Data :

How do I export nested attributes to CSV? i.e. I have a primary contact model, and I nested secondary contact into it. How do I export both as single CSV file?


wade0727 said over 3 years ago on Importing and Exporting CSV Data :
What's the best approach to this using the Kaminari Gem so that I can pull all rows in the index, not just the amount listed on the first page? ``` def index search = params[:term].present? ? params[:term] : nil @nodes = if search Node.search(search) else Node.order(:node).page(params[:page]) end respond_to do |format| format.html format.csv { send_data @nodes.to_csv, filename: "nodes-#{Date.today}.csv" } end end ```

David Kimura PRO said over 3 years ago on Importing and Exporting CSV Data :
You can do a check if the `request.format.csv?` is true and exclude the `page(params[:page])` method.

wade0727 said over 3 years ago on Importing and Exporting CSV Data :
Wow, that was easier than I thought it was going to be. Thank you!! :)

Luke Scott said 4 months ago on Importing and Exporting CSV Data :
I've got a situation where I'm trying to import a csv and its telling me Unknown Attribute, though I can see the attribute is there and correct.  I had a look with debugger and checked the contents of the customer and customer_hash and everything looks fine to me.  Yet when you try to update, unknown attribute 'account_number'.  Any thoughts?



Error:
unknown attribute 'account_number' for Customer. 

CustomersController:
def import
  Customer.import(params[:file])
  redirect_to customers_path, notice: "Customers Imported."
end

Customer Model:
  def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
      customer_hash = row.to_hash
      customer = find_or_create_by!(sla_number: customer_hash['sla_number'])
      customer.update(customer_hash)
    end
  end

Customers index:
 <%= form_tag import_customers_path, multipart: true, class: 'form-inline' do %>
   <%= file_field_tag :file, class: "" %>
   <%= submit_tag "Import CSV", class: "btn btn-info" %>
<% end %>



David Kimura PRO said 4 months ago on Importing and Exporting CSV Data :
Typically, when everything looks correct and I still cannot explain the problem, it is usually because of Spring. Did you just add the account number field or has it been there? Is it a column in the database?

Luke Scott said 4 months ago on Importing and Exporting CSV Data :
I added the account_number field fairly recently.

Showing in the schema:
  create_table "customers", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "account_number"
    t.string "sla_number"
    t.string "mobile_phone"
    t.string "work_phone"
    t.string "home_phone"
    t.string "municipality"
    t.string "main_product"
    t.string "contract_name"
    t.date "contract_from"
    t.date "contract_to"
  end

And looks to exist in the table when I browse it.

Stopped and started my rails s for good measure.

It's very strange and had my head spinning for a while trying to diagnose it.

David Kimura PRO said 4 months ago on Importing and Exporting CSV Data :
Try running this to see if that makes any difference
bin/spring stop

Luke Scott said 4 months ago on Importing and Exporting CSV Data :
Alas, I don't appear to have spring added

Luke Scott said 4 months ago on Importing and Exporting CSV Data :
  David Kimura  

I got it working!

  def self.import(file)
    CSV.foreach(file.path, headers: true, header_converters: :symbol) do |row|
      customer_hash = row.to_hash
      customer = find_or_create_by!(sla_number: customer_hash[:sla_number])
      customer.update(customer_hash)
    end
  end

Converting the headers to symbols and calling it as a symbol on the customer_hash did the trick.

Login to Comment