mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Generate add_index by default when giving type belongs_to or references
This commit is contained in:
parent
f176b2552e
commit
3b9120fa52
4 changed files with 34 additions and 3 deletions
|
@ -1,5 +1,27 @@
|
|||
*Rails 3.1.0 (unreleased)*
|
||||
|
||||
* When a model is generated add_index is added by default for belongs_to or references columns
|
||||
|
||||
rails g model post user:belongs_to will generate the following:
|
||||
|
||||
class CreatePosts < ActiveRecord::Migration
|
||||
def up
|
||||
create_table :posts do |t|
|
||||
t.belongs_to :user
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :posts, :user_id
|
||||
end
|
||||
|
||||
def down
|
||||
drop_table :posts
|
||||
end
|
||||
end
|
||||
|
||||
[Santiago Pastorino]
|
||||
|
||||
* Setting the id of a belongs_to object will update the reference to the
|
||||
object. [#2989 state:resolved]
|
||||
|
||||
|
|
|
@ -8,6 +8,10 @@ class <%= migration_class_name %> < ActiveRecord::Migration
|
|||
t.timestamps
|
||||
<% end -%>
|
||||
end
|
||||
|
||||
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
|
||||
add_index :<%= table_name %>, :<%= attribute.name %>_id
|
||||
<% end -%>
|
||||
end
|
||||
|
||||
def down
|
||||
|
|
|
@ -811,6 +811,8 @@ class CreateComments < ActiveRecord::Migration
|
|||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :comments, :post_id
|
||||
end
|
||||
|
||||
def self.down
|
||||
|
@ -819,7 +821,7 @@ class CreateComments < ActiveRecord::Migration
|
|||
end
|
||||
</ruby>
|
||||
|
||||
The +t.references+ line sets up a foreign key column for the association between the two models. Go ahead and run the migration:
|
||||
The +t.references+ line sets up a foreign key column for the association between the two models. And the +add_index+ line sets up an index for this association column. Go ahead and run the migration:
|
||||
|
||||
<shell>
|
||||
$ rake db:migrate
|
||||
|
|
|
@ -3,7 +3,7 @@ require 'rails/generators/rails/scaffold/scaffold_generator'
|
|||
|
||||
class ScaffoldGeneratorTest < Rails::Generators::TestCase
|
||||
include GeneratorsTestHelper
|
||||
arguments %w(product_line title:string price:integer)
|
||||
arguments %w(product_line title:string product:belongs_to user:references)
|
||||
|
||||
setup :copy_routes
|
||||
|
||||
|
@ -14,7 +14,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
|
|||
assert_file "app/models/product_line.rb", /class ProductLine < ActiveRecord::Base/
|
||||
assert_file "test/unit/product_line_test.rb", /class ProductLineTest < ActiveSupport::TestCase/
|
||||
assert_file "test/fixtures/product_lines.yml"
|
||||
assert_migration "db/migrate/create_product_lines.rb"
|
||||
assert_migration "db/migrate/create_product_lines.rb", /belongs_to :product/
|
||||
assert_migration "db/migrate/create_product_lines.rb", /add_index :product_lines, :product_id/
|
||||
assert_migration "db/migrate/create_product_lines.rb", /references :user/
|
||||
assert_migration "db/migrate/create_product_lines.rb", /add_index :product_lines, :user_id/
|
||||
|
||||
# Route
|
||||
assert_file "config/routes.rb" do |route|
|
||||
|
|
Loading…
Reference in a new issue