对于我的模型之一,我有一些应该自动填充的实例。 我该如何处理这个问题?
我必须具体说明这一点才有意义。 在我的应用程序中,我有一个名为 theme 的模型,其中包含小部件颜色主题信息。 我们提供了一些主题,但主要依靠用户创建自己的主题。 所以问题是:我的主题存储在哪里? 如果我将它们存储在主题数据库中,那么每当我在测试期间切换数据库或刷新数据库时,我都必须重新输入主题。这没什么大不了的,只是看起来很草率。
现在我将主题存储在控制器的哈希中。 问题在于,每个小部件都有一个主题,每个小部件都有一个 theme_id,而我们提供的主题没有 theme_id,因为它们没有存储在数据库中。
我知道这个问题的解决方案非常简单,但我想确保我的解决方案采用最佳编码实践。 有人对此有什么建议吗? 也许有一种方法可以在迁移或其他 rake 任务期间将条目添加到数据库中...
谢谢!
托尼
I have to be specific for this to make sense. In my application I have a model called theme which contains widget color theme information. We provide a few themes, but mainly rely on the user to create their own themes. So the question is: where do I store my themes? If I store them in the theme database, then anytime I switch databases or flush it during testing, I must re-enter the themes in. This is not a huge deal, it just seems sloppy.
Right now I have the themes stored in a Hash in the controller. The problem with that is since each widget has a theme, each widget has a theme_id, and there is no theme_id for our provided themes because they are not stored in the database.
I know that a solution to this issue is pretty simple, but I want to make sure my solution employs the best coding practices. Does anyone have any suggestions for this? Maybe there is a way to add entries into the database during a migration or other rake task...
Thank you!
Tony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在迁移中加载种子数据是最有意义的,也是我经常做的事情。 如果创建前几条记录确实是表正确初始化的一部分,即如果没有它们,您的表根本无法完成应用程序中所需的工作,那么它们就属于迁移。 Rake 任务非常适合捕获需要根据命令加载到应用程序中的数据集(例如演示),但如果始终需要某些记录,那么迁移就是最佳选择。
Loading seed data in the migrations makes the most sense and is something I have done often. If creating those first few records is truly part of the proper initialization of your table, i.e. if your table simply will no do the job you need in your application without them, then they belong in the migration. Rake tasks are great for capturing data sets that you need to load into the application on command (for example for a demo), but if certain records are going to be consistently required, the migration is the spot.
主题数据属于数据库。
为了进行测试,请使用固定装置或
setup
方法初始化主题数据。 对于开发和生产,您应该创建一种方法来使用初始主题数据为数据库播种。 自定义 Rake 任务对此很有用。 实际上,实际的主题数据可以以您希望的任何格式存储。 例如 SQL 脚本或 YML 固定装置。我使用以下 Rake 任务使用 db/seeddata 中 SQL 文件中保存的数据为数据库播种(将其放入项目的 /lib 目录下的 .rake 文件中):
The theme data belongs in the database.
For testing, initialize the theme data using fixtures or the
setup
method. For development and production, you should create a way to seed the database with the initial theme data. A custom Rake task is good for this. The actual theme data can be stored in any format you wish, really. For example SQL scripts or YML fixtures.I use the following Rake task to seed the database with data held in SQL files within db/seeddata (put this in a .rake file under your project's /lib directory):
您播种数据的想法似乎是可行的方法。 我喜欢你提出的方法,但是你觉得这里提出的方法怎么样:
http://railspikes.com/2008/2/1/loading-seed- data
它建议您让 ActiveRecord 处理数据播种,以便它可以验证数据。
你认为哪一个更好?
再次感谢!
托尼
Your idea to seed the data seems like the way to go. I like the method you proposed, but what do you think of the method proposed here:
http://railspikes.com/2008/2/1/loading-seed-data
It suggests that you let ActiveRecord handle seeding the data so that it can validate the data.
Which do you think is better?
Thanks again!
Tony