如何自动加载 rake 文件中的数据
我有一个 rake 任务,用值填充数据库。
这是一个示例:
Icon.create!( :category_id => category_id,
:name => "Wink",
:url => "#{url}wink.png",
:icon_for => icon_for )
我想做的是自动执行此操作,因为“name”属性只是文件夹中文件的名称,但大写且不结束,而“url”属性只是文件名。
其余的我用变量控制。
有没有一种方法可以像这样:
- 读取指定文件夹中的文件数量
- ,循环指定次数,并创建对象,在需要时插入文件名的值。
我该怎么做?
先感谢您。
Rails 3.0.7 红宝石 1.9.2 Mac OSX 10.6
I have a rake task that populates a database with values.
Here is a sample:
Icon.create!( :category_id => category_id,
:name => "Wink",
:url => "#{url}wink.png",
:icon_for => icon_for )
What I would like to do is automate this as the 'name' attribute is just the name of the file in a folder but Upper case and without ending while the 'url' attribute is just the file name.
The rest I control with variables.
Is there a way I could something like the this:
- read the number of files in a specified folder
- loop that number of times and create the object inserting the values for the file names where required.
How can I do this?
Thank you in advance.
Rails 3.0.7 Ruby 1.9.2 Mac OSX 10.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以在 Ruby 标准库的帮助下做到这一点。您应该了解 File 类 和 目录类。下面是您的代码的草图:
我将把它作为练习,让您弄清楚如何将文件名的第一个字母大写并去掉扩展名。
Yes, you do that with the help of Ruby's standard libraries. You should learn about the File class and the Dir class. Here's a sketch of what your code could look like:
I'll leave it as an excercise for you to figure out how to capitalize the first letter of the filename and chomp off the extension.