Luke Scott
Joined 9/23/2022
Luke Scott said over 1 year 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 %>



Luke Scott said over 1 year 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.

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

Luke Scott said over 1 year 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.

Luke Scott said 6 months ago on Gathering Questionnaire Responses :
Hi,

I've added a date_answer option to my selection.  All is working and its being stored in the answers JSON.  I'm having some trouble then displaying that info on a show or index page.

Its stored as {"10(1i)"=>"2023", "10(2i)"=>"10", "10(3i)"=>"9"} with the answers JSON, how would you go about pulling and displayin this information?

EDIT:

Nevermind, changed to using a date_field and now it stores as 2023-10-09 which can be directly output in the same was as a string.