Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke generators from code?

I wrote my own generator, from console its launched like this

rails generate ead_document TechnicalOpinion --document_type_id=1

It creates model and migration. I want to execute generator from my controller without using ruby system command. Is there any way to do that?

like image 733
Adrian Serafin Avatar asked Nov 02 '10 19:11

Adrian Serafin


1 Answers

Solution appears to be pretty simple:

This code in controller

Rails::Generators.invoke("ead_document", [@document_type.table_name.classify, "--document_type_id=#{@document_type.id}"])

is the same as this in console

rails generate ead_document TechnicalOpinion --document_type_id=1

In case you want to use it outside a controller, you may also want to require it explicitely:

require 'rails/generators'
like image 127
Adrian Serafin Avatar answered Oct 20 '22 20:10

Adrian Serafin