ruby - How to write Rake task to import data to Rails app? -
Target: A CRAN function (or other scheduled) to update the database with night export Event) data from existing system
The existing data in all data is created / updated / deleted, the website does not directly integrate with this system, therefore the Rail app needs to reflect updates in data export.
I have a file of .txt
5000 "ABC Manufacturing": "2222" "A134": "Another Product": "ABC Manufacturing": "ABC Manufacturing" "Entry 1": "Entry 2": "FOBr World": "2447" ...
All prices are enclosed in double quotation marks ( "
) that are different from the colon (:
)
:
-
id
: unique ID -
vs Buyer_name
: Any letter -
seller_id
: unique vendor ID; numerical
Seller's information is normalized in the current system Is not it the best practices here? Is it okay to remove products and sellers from the tables and rewrite the new data on every cycle? Or is it better to add new ones and add existing ones Update?
Note:
- This data will continue through the
command
night database importorder items
the data specified in the data file ID will need to be connected, so we can not depend on an auto-incremental primary key for each import; The unique alphanumeric ID will need to be used to be included in theproducts
toorder_items
. - Ideally, I would like the importer to normalize the seller's data
- I can not use vanilla SQL statements, so I think that I would like to
rake < To use the code, use
Product.create (...)
andthe seller. Creating (...)
style syntax. - This engine will be applied to the yard
Will not remove the table of products and vendors? Is this a rail app? If so, there are some very good activecrod helpers that will be helpful for you.
If you have a product active record model, then you can:
p = product .find_or_initialize_by_identifier (& lt; id that you get from the file Are & gt;) p.name = & lt; File from file & gt; P.size = & lt; From the size file & gt; Etc ... p.save!
find_or_initialize will see the product in the database by the id you specify, and if he can not find it, it will make a new one. The simple thing to do this like this is that ActiveRecord will only save the database if any data has changed, and accordingly it will automatically update any timestamp field in the table (Update_AT) . One more thing, because you will be seeing the identifier (ID from file), I will make sure to add an index to that field in the database.
To create a rake work to do a full job, I will add a rake file to the lib / tasks directory of my Rail app. We call this data. Rake
Inside the data. Rake, it will look something like this:
Namespace: Use data "Import data from data to data" Task: import = & gt; : Environment File = File File to open (& lt; import file; & gt;) file. Do all Line | Attrs = line.split (":") p = product.fund_or_oritial_disputation_functionor (attrs [0]) p. Name = etters [1] etc ... Pissau! P>
Comments
Post a Comment