如何使用Rails :: Generators.invoke("active_record:migration")生成创建表迁移文件? [英] How to generate create table migration file using Rails::Generators.invoke("active_record:migration")?
本文介绍了如何使用Rails :: Generators.invoke("active_record:migration")生成创建表迁移文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用此代码时
Rails::Generators.invoke("active_record:migration","create_tests",{:behavior=>:invoke, :destination_root=>Rails.root})
要生成迁移文件,我生成了以下类:
to generate migration file, I got the following class generated:
class CreateTests < ActiveRecord::Migration
def up
end
def down
end
end
但是我想要的是这个
class CreateTests < ActiveRecord::Migration
def change
create_table :tests do |t|
t.timestamps
end
end
end
我不知道我需要在invoke方法内传递哪些参数.
I'm not aware of what parameters I need to pass inside the invoke method.
推荐答案
您可以创建模板文件并将其用于生成目的.
You can create template file and use it for generation purposes.
class CoolMessageGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
argument :model_name, :type => :string, :default => 'CoolMessage'
def migration
template 'cool_migration.rb',
File.join('db', 'migrate', "#{Time.now.to_i}_create_#{model_name.tableize}.rb")
end
end
和template/cool_migration.rb在同一目录中:
and templates/cool_migration.rb in same directory:
class Create<%= model_name.pluralize %> < ActiveRecord::Migration
def change
create_table :<%= model_name.tableize %> do |t|
t.timestamps
end
end
end
这篇关于如何使用Rails :: Generators.invoke("active_record:migration")生成创建表迁移文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文