User selected rows when importing CSV to Rails - ruby-on-rails

Is there an accepted best practice to allow users to select the rows associated with columns when importing a CSV file in Ruby on Rails?
Such that they upload the file -> are given a confirmation screen with the list of column headers, and select inputs for each one (populated with the writeable attributes of the model) -> save each CSV row to the model, with the columns selected in the previous step.
Basically, I want the functionality shown here:
http://youtu.be/5Ddnu_NK_-o?t=50s
The way I would go about this, were there not a better way, would be something like this:
User uploads file
Parse CSV
Display first several rows
Display a form with select inputs for each column, populated by the writeable attributes, and cached file
For each of the POST column name parameters, match them to the column (while verifying that no column name is used twice)
Create a new record for each row
I confess that steps 4 and 5 are pretty daunting to me as a newbie to RoR, and it seems like this must be encountered a fair amount, but I haven't found any luck finding gems or tutorials on the subject.

Related

Storing CSV files to a database?

I'm trying to create an app that will store multiple csv files to a database. I have a working app going, and it will store csv files of a particular format. It looks through the header of the csv and stores each row of the csv with the column of the csv corresponding to the property of that row. For example, maybe the columns are date, school, major
if the following row is like this: 05/16/16 | Arizona State | English
then we'll store an object with the date, school, and major corresponding to above.
However, I need to deal with csv files that have different names. Some csv files might have the major column be named "Undergraduate major" and others might have it named "Field of study." Nevertheless, I want to be able to store all the csv files into my database even though the csv might have different names for the columns. What's the best way to approach this?

Is it safe to "search" creating db records in Rails?

I need to build a search form including some fields like "city, price range, key word and date". I saw this video which is recommending to create a "searches" table. Every time any user made a search, it creates a table row in the db, and show the results depending on the submitted fields.
Seems easy to build but is it safe? I mean, if this is used in the practical world, I think we also have to use "I'm not robot" from Google.
https://www.youtube.com/watch?v=QRE7KxIvUb4&t=29s
Any idea is welcome.

Delphi Combo Edit from CSV File

I have an application that will load a CSV file containing two columns. At program load I need to have the first column as items in a combo edit control. Once the user selects (or enters a value) I need to populate a label with the value from the second column. My thought was to use an in memory data set, FDMemTable which would be loaded at form create. Then once the user selects, or enters an item, I would run a query to pull the description. I've tried but have been unsuccessful with the simple loading of the combo edit.
Is this the best way to achieve the desired results, and is there samples similar to what I'm looking to do?
Read the file.
Parse it into an array of name/value pairs.
Populate the combo drop down list with the names.
When the combo's selection is changed to a new index, read the value from the array using that index. Update the label.
Database tables and queries seem somewhat over the top for a single simple task like this.

add a checkbox to each row inside an XLSX, then delete on upload

So ATM the user can download an xlsx document, and add new records to it, upload it to the application which updates the DB(with the new records)
I would like to put an option in the excel doc. like a check box (at the beginning of each row) or something like this which, if selected when the doc is uploaded again, it will delete that record from the DB.
I know the logic and how to delete the record but im not sure how to include something like a check box and relate it to each seperate row?
Any one have any ideas?
thank ye
You can use Checkboxes, although you would need to use an "xlsm" (macro-enabled workbook) to insert them. (you could do it without vba but manually inserting checkboxes and configuring them isnt going to be much fun)
Checkboxes can be "linked" to a cell in your workbook. You'd need to add a Checkbox to each line, and set the linked cell to another cell on that row (preferably all the same column).
You could also make it a lot simpler by doing away with the checkbox and just having a column that you set to true (or any non-empty value) and have your application interpret that to delete the records.
Both methods require you to have an extra column.
Any method will require either an extra column or another sheet with a single column to track the state of each line.

Creating a data visualization site with Rails

I have a very large excel spreadsheet that consists of a user name, a location, a date, and some fields of numbers, for example.
User,location,date,value1,value2,value3
Steve,NYC,2012,9,1,3
Steve,NYC,2011,3,3,2
Steve,CA,2011,1,2,0
Michael,CA,2012,10,3,2
Michael,CA,2011,10,2,0
How would I go about organizing a rails site such that one can view all the values for a certain user?
For example,
/users/steve/all
would display all the values in descending order of date where user=steve.
/users/steve/nyc
would display all the values in descending order of date where user=steve and location=nyc.
I think I would need to create a users model and import all the data from the excel into the database, but I'm lost about how to do that.
The application, in essence, would be a simple data visualizer. Maybe I have to separate the database and create a user has_many :locations and locations :belongs_to user, I'm not sure. I want the data to be viewed in all sorts of ways—maybe I want to display all the users from a certain location, or view all the locations of a certain user, etc...
I suggest setting up your model within your rails application first. Then, you can just write a rake task probably similar to this question or you can build it from scratch. There's also a railscast.
If you need to directly import from Excel (e.g. the excel sheets are uploade by a user). You can use one of the following gems to importing data from an Excel Sheet
roo Reads new and old excel format (+others)
spreadsheet Reads and writes old exel format
If you only have this one excel sheet it will be far easier to simply export the data to csv and follow the answers given in the stackoverflow question mentioned above.
As for the second part of your question on how to model your database you already had the right idea.
Easiest is to fully model what your domain looks like and transform the data accordingly.

Resources