views:

45

answers:

2

I'm find such a gem a whole day, but not find a good one. I want to write one, but I'm not able to do it.

The data in my database may be English text, which will be dump to a yml file with plain text. And some are non-English text, which will be binary type.

And both of them may have such code:

<% xxx %>

When I use rake db:fixtures:load to load them into database, error may occur: method xxx not found.

I wan't to find a good gem can handle this problem. Thank for any help


UPDATE

I have gave up finding such a gem. At first, I think it's a easy task, but now, after some researchings, I have to say, it's much harder than I expected.

+1  A: 

Err...I'm not sure if you actually need a gem for this? Rails natively can turn any model into YAML.

Let's say you have a model called "Objects". You could hit a route that looks like:

/objects.yaml

and you would get a giant text file of all your Objects in YAML form.

Of course, you would want to have something like:

respond_to do |format|
  format.yaml {render  :yaml => @objects}
end

in your restful controller.

If you'd rather not hit a route to do this, you can always do

@yaml = []
@objects.each do |object|
@yaml.push object.to_yaml
end

anywhere in ruby, which will give you an array of yaml objects, that you can then write to a file at your leisure.

I imagine that if rails itself is generating the yaml, then it would be able to then later load it as a fixture?

Jenny
@Jenny, when I dump the database to yaml files, I use `object.to_yaml` in rails enviroment. But, unfortunately, when load back, rails will treat it as erb code, not plain text. I have gave up finding such a gem, maybe it's mission impossible. Thanks for you answer.
Freewind
+1  A: 

The reason you are having problems is because the Fixture loader will pass your fixture through erb before loading the data. This means that if you have <% xxx %> in your yaml file then Rails will see that as erb and try to run a method called xxx.

There does not seem to be an easy way to turn off erb processing of fixtures. I have tried replacing the fixtures with CSV fixtures and this still does ERB pre-processing.

Without a simple answer I have to ask the question Why do you have these strings in your file?

Do you want them to be expanded by erb?

Steve Weet
@Steve, I agree with you, I tried a lot, and found it's impossible to solve this problem(If the content is English and short, to_yaml() will use plain text. If the content has non-English, to_yaml will use binary. Simply replacement won't work).
Freewind
My files contains such strings because the content in database are some code, which contains such erb code. I wish `rake db:dump:fixtrues` don't treat them as erb.
Freewind
I hoped to dump all the data of database to yml files, and load them back. But now, I have to say, this is a very difficult task(for example, the sequences won't dump to files, when you load back, the sequences of your database will have invalid current values). So I give up, turn to use database backup/restore tools.
Freewind