Freewind
Freewind

Reputation: 198238

Is there any gem can dump the data from and to yml files?

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.

Upvotes: 0

Views: 401

Answers (2)

Steve Weet
Steve Weet

Reputation: 28402

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?

Upvotes: 1

J.R.
J.R.

Reputation: 6049

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?

Upvotes: 1

Related Questions