是否有任何 gem 可以将数据从 yml 文件转储到 yml 文件?

发布于 2024-09-06 13:47:04 字数 405 浏览 1 评论 0原文

我一整天都在寻找这样的宝石,但没有找到好的。我想写一篇,但我做不到。

我的数据库中的数据可能是英文文本,它将转储到纯文本的 yml 文件中。有些是非英文文本,这将是二进制类型。

并且它们都可能有这样的代码:

<% xxx %>

当我使用rake db:fixtures:load将它们加载到数据库中时,可能会出现错误:method xxx not find

我不想找到一个好的宝石可以解决这个问题。感谢您的帮助


更新

我已经放弃寻找这样的宝石了。起初,我认为这是一项简单的任务,但现在,经过一些研究,我不得不说,这比我预期的要困难得多。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可可 2024-09-13 13:47:04

您遇到问题的原因是因为夹具加载器将在加载数据之前通过 erb 传递您的夹具。这意味着如果您有 <% xxx %>在你的 yaml 文件中,Rails 会将其视为 erb 并尝试运行名为 xxx 的方法。

似乎没有一种简单的方法可以关闭夹具的 erb 处理。我尝试用 CSV 装置替换装置,这仍然会进行 ERB 预处理。

如果没有简单的答案,我必须问一个问题:为什么你的文件中有这些字符串?

您希望 erb 扩展它们吗?

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?

夜清冷一曲。 2024-09-13 13:47:04

呃...我不确定你是否真的需要宝石? Rails 本身可以将任何模型转换为 YAML。

假设您有一个名为“对象”的模型。您可以选择如下所示的路线:

/objects.yaml

您将获得一个包含 YAML 形式的所有对象的巨大文本文件。

当然,您希望

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

在您的宁静控制器中拥有类似的东西。

如果您不想直接执行此操作,则始终可以

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

在 ruby​​ 中的任何位置执行此操作,这将为您提供一组 yaml 对象,然后您可以在闲暇时将其写入文件。

我想如果 Rails 本身生成 yaml,那么稍后它就可以将其作为固定装置加载?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文