0

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.

4

2 回答 2

0

也阅读有关Dir对象的信息。

这是获取所有文件的示例。

Dir["Tel/a_*/poi1/names.csv"] 返回一个包含所有文件路径的数组。

于 2013-07-16T19:03:54.363 回答
0

像这样的东西应该给你文件夹中的所有文件名,供你操作:

Dir["Tel/**/**/*.csv].each do |file|
  * update attribute of your model with the path of the file
end
于 2013-07-16T19:04:47.777 回答