Prev | Current Page 408 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


In addition, model associations are not included in the default YAML output.
Because of the minimal configuration options, if you need to customize the actual data
output from to_yaml, it may be best write your own custom YAML output methods.
Importing YAML
The Ruby YAML parser has a transform method that converts the parsed YAML into Ruby objects.
This method can be used to convert the YAML generated by the to_yaml method back into
Active Record objects.
Given a file named accounts.yml, the contents of which are the YAML output by the command
Account.find(:all).to_yaml, we can create a simple program to read in the file and
save the objects to the database. The relevant code follows:
CHAPTER 7 ?–  WORKING WITH LEGACY SCHEMA 184
accounts_yaml_string = File.read("accounts.yml")
accounts = YAML::parse(accounts_yaml_string).transform
accounts.each { |account| account.save }
Exporting CSV
Like YAML, CSV is a data type that is supported by Active Record fixtures, which are used for
unit, functional, and integration tests.


Pages:
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420