1

I am trying to export my development sqlite3 database into my test database. In order to do this I first exported my model in rails console and saved it to a file.

> MyModel.all.to_yaml   # this was saved to mymodels.yml

Now when I run rspec it fails while trying to parse mymodels.yml. The error I get is:

Failure/Error: Unable to find matching line from backtrace
 a YAML error occurred parsing /Users/MakeM/MyProject1/spec/fixtures/mymodels.yml.

Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html The exact error was: NoMethodError: undefined method `keys' for nil:NilClass

Any idea why I am getting this error? One thing I should mention is that the yaml that is output by to_yaml looks a bit strange to me. Here's part of it:

--- 
- !ruby/object:MyModel 
  attributes: 
    id: 133
    book: FirstBook
    chapters: 50
    created_at: 2010-10-06 05:03:15.709931
    updated_at: 2010-10-06 05:03:15.709931
    abbr: FB
  attributes_cache: {}

  changed_attributes: {}

  destroyed: false
  marked_for_destruction: false
  new_record: false
  previously_changed: {}

  readonly: false
MakeM
  • 107
  • 5

1 Answers1

0

It's better to use only data save on your database not all data useless So try to generate your Yaml like that :

MyModel.all.map(&:attributes).to_yaml

Warning, this technics can explode your RAM if you have a lot of data. Think to generate your haml with limit / offset. or with paginated_each from will_paginate.

shingara
  • 46,608
  • 11
  • 99
  • 105
  • Thanks! What exactly does the .map(&:attributes) do? I tried googling but didn't come up with anything to help me understand. – MakeM Nov 20 '10 at 11:44
  • This doesn't seem to work either. I get the same "Failure/Error: Unable to find matching line from backtrace Bad data for..." error. Though the yaml does look much closer to what it what it should look like. – MakeM Nov 20 '10 at 11:59
  • map(&:attributes) Call to all model the attributes method and the generate an Array of result – shingara Nov 20 '10 at 12:19
  • after doing some research i think this is a bug related to rspec 2 or spork. marking your answer correct since it's what helped me get the correct yaml format. thanks! – MakeM Nov 20 '10 at 12:26
  • This is very old, but for sake of Google etc... - array.map(&:foo) is a Rubyism explained in the high-rated answer here: http://stackoverflow.com/questions/2259775/what-do-you-call-the-operator-in-ruby – Andrew Hodgkinson Oct 01 '13 at 01:00