Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add fkey attributes to `join_table` migration generator #7097

Merged
merged 1 commit into from

3 participants

@lexmag

In continuation of PR #7028

@rafaelfranca

Rails doesn't generate foreign keys. Why we should add this now?

@lexmag

@josevalim what do you think about adding this syntax to references/belongs_to migrations?

rails g model Product supplier_id:references
@rafaelfranca

Hmmm, I saw that you are not generating foreign key constraint, only generating the key name.

@rafaelfranca

Ok. Seems good.

@josevalim josevalim merged commit 993e164 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 18, 2012
  1. @lexmag
This page is out of date. Refresh to see the latest.
View
6 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -204,11 +204,11 @@ def create_join_table(table_1, table_2, options = {})
join_table_name = find_join_table_name(table_1, table_2, options)
column_options = options.delete(:column_options) || {}
- column_options.reverse_merge!({:null => false})
+ column_options.reverse_merge!(null: false)
- t1_column, t2_column = [table_1, table_2].map{ |t| "#{t.to_s.singularize}_id" }
+ t1_column, t2_column = [table_1, table_2].map{ |t| t.to_s.singularize.foreign_key }
- create_table(join_table_name, options.merge!(:id => false)) do |td|
+ create_table(join_table_name, options.merge!(id: false)) do |td|
td.integer t1_column, column_options
td.integer t2_column, column_options
yield td if block_given?
View
2  activerecord/lib/active_record/migration/join_table.rb
@@ -8,7 +8,7 @@ def find_join_table_name(table_1, table_2, options = {})
end
def join_table_name(table_1, table_2)
- [table_1, table_2].sort.join("_").to_sym
+ [table_1, table_2].sort.join("_")
end
end
end
View
12 activerecord/lib/rails/generators/active_record/migration/migration_generator.rb
@@ -21,7 +21,7 @@ def set_local_assigns!
when /join_table/
if attributes.length == 2
@migration_action = 'join'
- @join_tables = attributes.map(&:name)
+ @join_tables = attributes.map(&:plural_name)
set_index_names
end
@@ -30,9 +30,17 @@ def set_local_assigns!
def set_index_names
attributes.each_with_index do |attr, i|
- attr.index_name = [attr, attributes[i - 1]].map{ |a| :"#{a.name.singularize}_id"}
+ attr.index_name = [attr, attributes[i - 1]].map{ |a| index_name_for(a) }
end
end
+
+ def index_name_for(attribute)
+ if attribute.foreign_key?
+ attribute.name
+ else
+ attribute.name.singularize.foreign_key
+ end.to_sym
+ end
end
end
end
View
10 railties/lib/rails/generators/generated_attribute.rb
@@ -90,8 +90,12 @@ def default
end
end
+ def plural_name
+ name.sub(/_id$/, '').pluralize
+ end
+
def human_name
- name.to_s.humanize
+ name.humanize
end
def index_name
@@ -102,6 +106,10 @@ def index_name
end
end
+ def foreign_key?
+ !!(name =~ /_id$/)
+ end
+
def reference?
self.class.reference?(type)
end
View
2  railties/test/generators/migration_generator_test.rb
@@ -169,7 +169,7 @@ def test_add_migration_with_references_options
def test_create_join_table_migration
migration = "add_media_join_table"
- run_generator [migration, "artists", "musics:uniq"]
+ run_generator [migration, "artist_id", "musics:uniq"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |up|
Something went wrong with that request. Please try again.