使用 Rails 引擎的 rake 将示例数据添加到数据库
我正在通过创建一个分类引擎来尝试 Rails 引擎,用户可以在其中查看/发布/回复分类。
主应用程序包含用于用户身份验证和配置文件的代码,同时我创建了一个引擎来处理分类功能。
现在我想向分类引擎的数据库添加一些示例数据。所以我在'vendor/plugins/classifieds/lib/tasks'中创建了一个名为'sample_classifieds_data.rake'的rake文件,并在'vendor/plugins/classifieds/lib/tasks/sample_classifieds_data'中添加了yml文件
rake文件的代码可以在此处找到示例 yml 文件: http://gist.github.com/216776
现在问题是,当我运行 rake 任务时,没有抛出任何错误,但值没有填充到数据库中。
有什么想法吗? BTW,它是开发环境,数据库是开发数据库。
我运行了一个类似的 rake 任务来填充有效的数据库中的示例用户。该 rake 文件“sample_data.rake”的位置位于“lib/tasks”中。
I am trying out Rails engines by creating a classifieds engine where users can view/post/reply to classifieds.
The main application contains code for user authentication and profiles while there is an engine which I have created which will deal with the classifieds functionality.
Now I want to add some sample data to the database for the classifieds engine. So I created a rake file called 'sample_classifieds_data.rake' in 'vendor/plugins/classifieds/lib/tasks' and I added the yml files in 'vendor/plugins/classifieds/lib/tasks/sample_classifieds_data'
The code of the rake file and a sample yml file can be found here: http://gist.github.com/216776
Now the problem is that when I run the rake task, no error is being thrown but the values are not getting populated in the database.
Any ideas? BTW, it is development environment and the database is the development database.
I ran a similar rake task to populate sample users in the database which worked. the location of that rake file 'sample_data.rake' was located in 'lib/tasks'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Rails Edge 中,您可以使用
rake db:seed
功能将数据添加到您的库中。 查看提交。使用方法非常简单。
创建一个
db/seeds.rb
文件。并将您想要为数据库播种的任何代码放入其中。
例如:
当您想要为数据库播种时,您可以执行 rake db:seed ,
如果出于任何原因您不希望使用边缘(这在生产环境中是可以理解的) ,您可以使用 Seed Fu 插件,它非常适合您。
In rails edge, you can use the
rake db:seed
feature to add datas to your base. See the commit.The use is pretty simple.
Create a
db/seeds.rb
file.And put whatever code you want to seed your database in it.
For example :
And when you want to seed your database, you could do a
rake db:seed
If, for any reason, you do not wish to use edge (which would be comprehensible in a production environment), you can use the Seed Fu plugin, which quite does the trick for you.
你的任务看起来不错。唯一会导致您的任务无提示失败的是您传递给 Fixture.new 的文件不指向 yml 或 csv 文件。
通过修改 put 语句进行双重检查,以打印其导入的文件的完整路径,并将其打印内容与目录结构进行比较。
例如,如果您的装置文件以大写字母开头,事情会默默地失败吗?类别.yml 而不是类别.yml
Your task looks good. About the only thing would cause your task to fail silently is that the file you're passing to Fixture.new does not point to a yml or csv file.
Double check by modifying the put statement to print the full path of the file it imported, and compare what it prints against your directory structure.
For example, things will fail silently if your fixture files start with a capital letter? Categories.yml instead of categories.yml
Rails 2.3.4 中添加了 db:seed 任务。所以不需要跑边。
http://weblog.rubyonrails.org/ 2009/9/4/ruby-on-rails-2-3-4
The db:seed task was added in Rails 2.3.4. So no need to run edge.
http://weblog.rubyonrails.org/2009/9/4/ruby-on-rails-2-3-4