开发者

how do I invoke rails model generator from my custom generator

I am getting started writing my own generators. I am finally to the place where I am seeing that I do a lot of the same things when I create rails apps.

Here is what my generator looks like so far.

class FbScaffoldGenerator < Rails::Generators::Base
  source_root File.expand_path('../templates', __FILE__)
  argument :model_name, :type => :string, :default => "fb_item"

  def create_models

  end

  private

  def mod_name
    model_name.underscore
  end
end

as you can tell I haven't gotten far. In the create_models method I would like to take the model_name passed in and invoke the rails mode开发者_如何学编程l generator, pass it the name and also define some fields along the way. I will also create a couple other models at the same time I am doing this so knowing how to invoke any of the pre defined generators will be really helpful.


use

def create_models
  invoke :model, [model_name, ["name:string", "age:integer:index"]]
end

if you want to skip some parts (say, fixtures) you can do

def create_models
  invoke :model, [model_name, ["name:string", "age:integer:index"]], ["--skip-fixtures"]
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜