以编程方式调用active_record生成器:​​如何传递父参数?

时间:2011-07-22 17:18:35

标签: ruby-on-rails-3 generator

在所有这些情况下,忽略父参数:

Rails::Generators.invoke("active_record:model", [name, ["list_order:string", "name:string"], ["parent=Ecm::Toplist::Base"]])
Rails::Generators.invoke("active_record:model", [name, ["list_order:string", "name:string"], ["--parent=Ecm::Toplist::Base"]])
Rails::Generators.invoke("active_record:model", [name, ["list_order:string", "name:string"], "parent=Ecm::Toplist::Base"])
Rails::Generators.invoke("active_record:model", [name, ["list_order:string", "name:string"], "--parent=Ecm::Toplist::Base"])

这是一个错误,还是我错过了什么?

1 个答案:

答案 0 :(得分:0)

正确的方式将是

Rails::Generators.invoke("active_record:model", [name, "list_order:string", "name:string"], { parent: "Ecm::Toplist::Base"})

但是请注意,根据this,一旦指定:parent,就不会调用迁移。

使用迁移生成模型的正确方法是

Rails::Generators.invoke("active_record:model", [name, "list_order:string", "name:string"], {migration: true, timestamps: true})