I must be specific with this to create sense. During my application I've got a model known as theme which consists of widget color theme information. We offer a couple of styles, but mainly depend around the user to produce their very own styles. So now you ask ,: where will i store my styles? Basically store them within the theme database, then anytime I switch databases or flush it throughout testing, I have to re-go into the styles in. This isn't an enormous deal, it simply appears sloppy.

At this time I've the styles saved inside a Hash within the controller. The issue with that's since each widget includes a theme, each widget includes a theme_id, and there's no theme_id for the provided styles since they're not saved within the database.

I understand that a strategy to this problem is fairly simple, but I wish to make certain my solution utilizes the very best coding practices. Does anybody have recommendations for this? Maybe there's a method to add records in to the database throughout a migration or any other rake task...

Thanks!

Tony

The theme data goes within the database.

For testing, initialize the theme data using fittings or even the setup method. For development and production, you need to create a method to seed the database using the initial theme data. A custom Rake task will work for this. The particular theme data could be saved in almost any format you want, really. For instance SQL scripts or YML fixtures.

I personally use the next Rake task to seed the database with data locked in SQL files within db/seeddata (put this inside a .rake file beneath your project's /lib directory):

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

Loading seed data within the migrations helps make the most sense and it is something I've done frequently. If creating individuals first couple of records is really area of the proper initialization of the table, i.e. in case your table simply won' get the job done you'll need inside your application without one, they belong within the migration. Rake tasks are ideal for taking data sets you need to load in to the application on command (for instance for any demo), but when certain records will be consistently needed, the migration may be the place.

Your idea to seed the information appears like what you want. I love the technique you suggested, but exactly what do you think about the technique suggested here: http://railspikes.com/2008/2/1/loading-seed-data

It indicates that you simply let ActiveRecord handle seeding the information to ensure that it may validate the information.

Which do you consider is much better?

Many thanks!

Tony