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?
I tried your scenario and I did not have any issues. I did the following changes to your YAML file creation logic:
I was able to retrieve the
questionslist as follows: