diff options
Diffstat (limited to 'activerecord/lib/rails')
3 files changed, 15 insertions, 5 deletions
diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb index 126b6f434b..ce8d7eed42 100644 --- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb @@ -1,4 +1,11 @@ class <%= migration_class_name %> < ActiveRecord::Migration +<%- if migration_action == 'add' -%> + def change +<% attributes.each do |attribute| -%> + add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %> +<%- end -%> + end +<%- else -%> def up <% attributes.each do |attribute| -%> <%- if migration_action -%> @@ -14,4 +21,5 @@ class <%= migration_class_name %> < ActiveRecord::Migration <%- end -%> <%- end -%> end +<%- end -%> end diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index c75abd043c..f7caa43ac8 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -10,6 +10,7 @@ module ActiveRecord class_option :migration, :type => :boolean class_option :timestamps, :type => :boolean class_option :parent, :type => :string, :desc => "The parent class for the generated model" + class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns" def create_migration_file return unless options[:migration] && options[:parent].nil? diff --git a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb b/activerecord/lib/rails/generators/active_record/model/templates/migration.rb index 70e064be21..4f81a52fd0 100644 --- a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/model/templates/migration.rb @@ -1,5 +1,5 @@ class <%= migration_class_name %> < ActiveRecord::Migration - def up + def change create_table :<%= table_name %> do |t| <% for attribute in attributes -%> t.<%= attribute.type %> :<%= attribute.name %> @@ -8,9 +8,10 @@ class <%= migration_class_name %> < ActiveRecord::Migration t.timestamps <% end -%> end - end - - def down - drop_table :<%= table_name %> +<% if options[:indexes] -%> +<% attributes.select {|attr| attr.reference? }.each do |attribute| -%> + add_index :<%= table_name %>, :<%= attribute.name %>_id +<% end -%> +<% end -%> end end |