开发者

to_yaml file not loading properly in rspec (rails project)

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 bi开发者_StackOverflowt 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜