在Rails 3生成器中多次调用任务 [英] Call task more than once in Rails 3 generator
问题描述
我正在编写创建两个不同模型的Rails 3生成器.这是我要执行的操作的非常简化的示例:
I'm writing a Rails 3 generator that creates two different models. Here's a very simplified example of what I'm trying to do:
def my_generator_task
invoke "model", ["foo"]
invoke "model", ["bar"]
end
问题在于 Thor调用方法仅调用一次任务,因此不会再进行对模型"任务的第二次调用,也不会创建栏"模型.有谁知道实现此目标的优雅方法,最好是不会破坏使用生成器运行"rails destroy"功能的方法?
The problem is that the Thor invoke method only invokes a task once, so the second call to the "model" task never happens and the "bar" model is never created. Does anyone know an elegant way to accomplish this, preferably in a way that doesn't break the ability to run "rails destroy" with the generator?
推荐答案
再想一想,通过这种方式还可以在不迁移的情况下运行多个模型生成器
One more thought, this way it is also possible to run multiple model generator without migration
Rails::Generators.invoke("active_record:model", ["foo", "--no-migration" ])
Rails::Generators.invoke("active_record:model", ["bar", "--no-migration" ])
这篇关于在Rails 3生成器中多次调用任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!