是否有任何 gem 可以将数据从 yml 文件转储到 yml 文件?
我一整天都在寻找这样的宝石,但没有找到好的。我想写一篇,但我做不到。
我的数据库中的数据可能是英文文本,它将转储到纯文本的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到问题的原因是因为夹具加载器将在加载数据之前通过 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?
呃...我不确定你是否真的需要宝石? Rails 本身可以将任何模型转换为 YAML。
假设您有一个名为“对象”的模型。您可以选择如下所示的路线:
您将获得一个包含 YAML 形式的所有对象的巨大文本文件。
当然,您希望
在您的宁静控制器中拥有类似的东西。
如果您不想直接执行此操作,则始终可以
在 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:
and you would get a giant text file of all your Objects in YAML form.
Of course, you would want to have something like:
in your restful controller.
If you'd rather not hit a route to do this, you can always do
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?