对于我的一个模型,我有几个实例应该是自动填充的。我该如何处理?

时间:2021-07-21 07:27:38

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.

我必须要明确这一点才有意义。在我的应用程序中,我有一个名为theme的模型,它包含小部件颜色主题信息。我们提供了一些主题,但主要依靠用户创建自己的主题。所以问题是:我在哪里存放我的主题?如果我将它们存储在主题数据库中,那么每当我切换数据库或在测试期间刷新它时,我必须重新输入主题。这不是什么大问题,只是看起来很草率。

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.

现在我将主题存储在控制器的散列中。这是因为每个小部件都有一个主题,每个小部件都有一个theme_id,我们提供的主题没有theme_id,因为它们没有存储在数据库中。

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...

我知道这个问题的解决方案非常简单,但是我想确保我的解决方案采用了最佳的编码实践。有人对此有什么建议吗?也许有一种方法可以在迁移或其他rake任务期间向数据库中添加条目……

Thank you!

谢谢你!

Tony

托尼

3 个解决方案

#1


4  

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.

在迁移过程中加载种子数据是最有意义的,也是我经常做的事情。如果创建前几个记录是正确初始化表的一部分,也就是说,如果没有它们,表就不能完成应用程序中需要的工作,那么它们就属于迁移。Rake任务非常适合于捕获需要加载到应用程序中的数据集(例如演示),但是如果某些记录是始终需要的,那么迁移就是spot。

#2


3  

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.

对于测试,使用fixture或setup方法初始化主题数据。对于开发和生产,您应该创建一种方法,用最初的主题数据为数据库添加种子。自定义Rake任务对这个很好。实际的主题数据可以以任何您希望的格式存储。例如SQL脚本或YML fixture。

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):

我使用下面的Rake任务将数据库中的数据放在db/seeddata中的SQL文件中(将它放在项目/lib目录下的.rake文件中):

namespace :db do
  desc "Load seed fixtures (from db/seeddata) into the current environment's database." 
  task :seed => :environment do
    require 'yaml'
    config = YAML::load(open("#{RAILS_ROOT}/config/database.yml"))["#{RAILS_ENV}"]
    Dir.glob(RAILS_ROOT + '/db/seeddata/*.sql').each do |file|
      cmd = "mysql -u #{config['username']} -p#{config['password']} -h #{config['host']} #{config['database']} < #{file}"
      `#{cmd}`
    end
  end
end

#3


1  

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

你为数据播种的想法似乎是正确的。我喜欢你提出的方法,但是你觉得这里提出的方法怎么样:http://railspikes.com/2008/2/1/loadingseeed -data

It suggests that you let ActiveRecord handle seeding the data so that it can validate the data.

它建议您让ActiveRecord处理数据的播种,以便它可以验证数据。

Which do you think is better?

你认为哪个更好?

Thanks again!

再次感谢!

Tony

托尼

#1


4  

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.

在迁移过程中加载种子数据是最有意义的,也是我经常做的事情。如果创建前几个记录是正确初始化表的一部分,也就是说,如果没有它们,表就不能完成应用程序中需要的工作,那么它们就属于迁移。Rake任务非常适合于捕获需要加载到应用程序中的数据集(例如演示),但是如果某些记录是始终需要的,那么迁移就是spot。

#2


3  

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.

对于测试,使用fixture或setup方法初始化主题数据。对于开发和生产,您应该创建一种方法,用最初的主题数据为数据库添加种子。自定义Rake任务对这个很好。实际的主题数据可以以任何您希望的格式存储。例如SQL脚本或YML fixture。

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):

我使用下面的Rake任务将数据库中的数据放在db/seeddata中的SQL文件中(将它放在项目/lib目录下的.rake文件中):

namespace :db do
  desc "Load seed fixtures (from db/seeddata) into the current environment's database." 
  task :seed => :environment do
    require 'yaml'
    config = YAML::load(open("#{RAILS_ROOT}/config/database.yml"))["#{RAILS_ENV}"]
    Dir.glob(RAILS_ROOT + '/db/seeddata/*.sql').each do |file|
      cmd = "mysql -u #{config['username']} -p#{config['password']} -h #{config['host']} #{config['database']} < #{file}"
      `#{cmd}`
    end
  end
end

#3


1  

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

你为数据播种的想法似乎是正确的。我喜欢你提出的方法,但是你觉得这里提出的方法怎么样:http://railspikes.com/2008/2/1/loadingseeed -data

It suggests that you let ActiveRecord handle seeding the data so that it can validate the data.

它建议您让ActiveRecord处理数据的播种,以便它可以验证数据。

Which do you think is better?

你认为哪个更好?

Thanks again!

再次感谢!

Tony

托尼