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.
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"]
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?
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
```
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
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?
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.
The
*
is a splat operator. So, take the following example.http://blog.honeybadger.io/rub...
The
!
at the end offind_or_create_by
will raise an exception on validation errors.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?
Error:
CustomersController:
Customer Model:
Customers index:
Showing in the schema:
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.
I got it working!
Converting the headers to symbols and calling it as a symbol on the customer_hash did the trick.