views:

465

answers:

3

I'm trying to save some lookup table data out to a YAML file so that later when I need to set up my app on a different machine I can load the data in as seed data.

The data is stuff like select options, and it's pretty much set, so no worries about the live data changing between serializing and deserializing.

I have output the data like this...

file = File.open("#{RAILS_ROOT}/lib/tasks/questions/questions.yml", 'w')
questions = Question.find(:all, :order => 'order_position')
file << YAML::dump(questions)
file.close()

And I can load the file like this...

questions = YAML.load_file('lib/tasks/questions/questions.yml')

However, when I try to save a question I get this error...

>> questions[0].save
NoMethodError: undefined method `save' for #<YAML::Object:0x2226b84>

What is the correct way to do this?

+1  A: 

If you're using Rails 2.3.4 (or above), they have a seeds.rb file that can be found in your applications db folder. This lets you define basic active record creates, and when you've set up your new project, you can simply call:

rake db:seed

There is an excellent Railscast on it here, and a good blog post about it here. If you're not using Rails 2.3.4 (Or, ideally, 2.3.5), I highly recommend updating for these cool features, and addition security/bug fixes.

Mike Trpcic
Thanks, yes that is a great feature. I want to store the data in a YAML file and load it using `seeds.rb`. There is enough data that hard-coding the model instantiations directly in `seeds.rb` would be awkward.
Ethan
seeds.rb is just a ruby file. You can load YAML inside of seeds.rb as described here (http://ruby-doc.org/core/classes/YAML.html), and simply loop through the array(s) while doing a Model.create().
Mike Trpcic
+2  A: 

Create a seed.yml file in db directory. Add a YAML document for each model you want to create. This document should contain a list of hash. Each hash should contain model attributes.

  users:
      -   login: jake
          password: jake123
          password_confirmation: jake123
          first_name: Jake
          last_name: Driver

      -   login: Jane
          password: jane123
          password_confirmation: jane123
          first_name: Jane
          last_name: McCain

  categories:

  products:

In your seed.rb file

seed_file = File.join(Rails.root, 'db', 'seed.yml')
config = YAML::load_file(seed_file)
User.create(config["users"])
Category.create(config["categories"])
Product.create(config["products"])
KandadaBoggu
A: 

I tried your scenario and I did not have any issues. I did the following changes to your YAML file creation logic:

File.open("#{RAILS_ROOT}/lib/tasks/questions/questions.yml", 'w') do |file|
  questions = Question.find(:all, :order => 'order_position')
  # pass the file handle as the second parameter to dump
  YAML::dump(questions, file)
end

I was able to retrieve the questions list as follows:

questions = YAML.load_file("#{RAILS_ROOT}/lib/tasks/questions/questions.yml")
KandadaBoggu