update database with a ruby function - ruby-on-rails

I have a series of folders that all have files I need linked to the database (via there file path). One option I could do is manually insert all the file paths into my database, however, this can be painful as the number of folders will keep increasing and manual uploading will take too much time.
Is there a way to write a ruby helper function that will search these folders and automatically add the path to the files into a column in my database?
All the file paths have a recognizable pattern, for example:
Tel/a_1/poi1/names.csv
Tel/a_2/poi1/names.csv
Tel/a_3/poi1/names.csv
I need a function that will occupy a field in my database with the path of each of these names.csv files. Very new to ruby and rails, so any help is greatly appreciated. Also, please let me know if anything is unclear.

Something like this should give u all the filenames in the folder, for you to manipulate:
Dir["Tel/**/**/*.csv].each do |file|
* update attribute of your model with the path of the file
end

Read about the Dir object too.
Thats a example to get all files.
Dir["Tel/a_*/poi1/names.csv"] return a Array with path about all files.

Related

What is a seed file?

I have been given a task to upload a file and store it in a different location. And I need to return a seed file which should be used to retrieve the file again.
I'm done with all sorts of things but I can't get "what is a seed file and how can generate it and how can I retrieve the file later with that"
can someone help me with this?
Thanks for helping!
Not sure of your context, but seed is usually a file (or a template) that contains some default values to begin with.

How to create Trailer Record in Flat File using SSIS

I am looking for some help on how to create a trailer record in a flat file using SSIS, I have create a SSIS package that creates a custom header and loads other record from the database into the flat file, it is a fixed width flat file. Now at the end of the file I want to create a Trailer Record along with some static text and Record count. I tried looking on to google but could not get any good example. Any help is much appreciated.
Use a Script Task. Take the file path of the fixed width flat file that you have obtained as a input variable. Once withing the script task use the .Net coding to append the data that you need. I have written a post on it - https://karbimantras.wordpress.com/2016/08/12/adding-record-count-to-flat-file/

rename file name with eloquent way

File.rename(blog_path + '/' + project_path, File.expand_path(topic_name, blog_path))
I use these code to rename ruby file name, but I think there is a better way to write this functionality with less code since it includes blog_path two times.
The code is OK, but I think there is no need to expand_path here - this method creates an absolute path from the the relative one.
Also, it is good to use File.join to create a path instead just concatenate it with slash - it will be completely OS independent. So I would write your code like this:
File.rename(File.join(blog_path, project_path), File.join(blog_path, topic_name))
Or if you want to get rid of doubled blog_path, change working directory before doing a rename:
Dir.chdir(blog_path)
File.rename(project_path, topic_name)
More info on working with files and directories in Ruby you can find in the article: Ruby for Admins: Files and Directories.

filename..extension // Paperclip reprocess

Some kind of problem I can't resolveā€¦
In some app, a method called on :before_create was prefixing the file's extension with a double-dot (ex. /images/13402/medium/hey-1..jpg)
The problem is fixed for the new ones, but nothing occurs when I apply a reprocess! on the old ones; and I'd like to know if anyone could help about it
Reprocess / refresh only takes your original image and recreates the defined styles in your model class. So if your original image contains a file path with double dots in it, these are also applied to the generated styles. You have to clean up your original files and the stored file paths in your model records.
The only way I know would be to write a little script to modify this. Basically
foreach image
strip out double dots from original file name
rename file
store new file path in model record
end
And then rake paperclip:refresh

Ruby on Rails - how to seed from a file "the right way"?

I have an XML file containing data and I can easily parse it to insert the data into my rails database. The only problem is - in which directory should the file go into ("public" seems wrong, since the initial data in the database should not be public), and how do I refer to that file in the seeds.rb file (i.e. what prefix will guarantee that that file will be found).
This is a somewhat silly question but I haven't touched rails for a time now and they keep changing the directory structure...
I'd simply put the file in a folder like /db/data_source because seeds.rb lives in /db so it keeps overall logic.
You can reference any file using:
"#{Rails.root}/path/to/file"

Resources