diff options
author | Stefan Kanev <stefan.kanev@gmail.com> | 2014-07-18 19:46:53 +0300 |
---|---|---|
committer | Stefan Kanev <stefan.kanev@gmail.com> | 2014-07-18 19:46:53 +0300 |
commit | a013b082aa4459762b55bbc6c59a4f072eef8224 (patch) | |
tree | 4161fe1a062ea26ed7d3feed70c020834f229e5b /activerecord/lib/active_record | |
parent | 843b8c0b8c8cccd8d1432060dfc79a8edcc4ed2c (diff) | |
download | rails-a013b082aa4459762b55bbc6c59a4f072eef8224.tar.gz rails-a013b082aa4459762b55bbc6c59a4f072eef8224.tar.bz2 rails-a013b082aa4459762b55bbc6c59a4f072eef8224.zip |
create_join_table uses same logic as HABTM reflections
Before this change, create_join_table would not remove the common prefix
in the join table name, unlike ActiveRecord::Reflections. A HABTM
between Music::Artist and Music::Record would use a table
music_artists_records, while create_join table would create
music_artists_music_records.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/migration/join_table.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/model_schema.rb | 11 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 2 |
3 files changed, 13 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/migration/join_table.rb b/activerecord/lib/active_record/migration/join_table.rb index ebf64cbcdc..05569fadbd 100644 --- a/activerecord/lib/active_record/migration/join_table.rb +++ b/activerecord/lib/active_record/migration/join_table.rb @@ -8,7 +8,7 @@ module ActiveRecord end def join_table_name(table_1, table_2) - [table_1.to_s, table_2.to_s].sort.join("_").to_sym + ModelSchema.derive_join_table_name(table_1, table_2).to_sym end end end diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index 092c3b4fb7..850220babd 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -55,6 +55,17 @@ module ActiveRecord delegate :type_for_attribute, to: :class end + # Derives the join table name for +first_table+ and +second_table+. The + # table names appear in alphabetical order. A common prefix is removed + # (useful for namespaced models like Music::Artist and Music::Record): + # + # artists, records => artists_records + # records, artists => artists_records + # music_artists, music_records => music_artists_records + def self.derive_join_table_name(first_table, second_table) # :nodoc: + [first_table.to_s, second_table.to_s].sort.join("\0").gsub(/^(.*_)(.+)\0\1(.+)/, '\1\2_\3').gsub("\0", "_") + end + module ClassMethods # Guesses the table name (in forced lower-case) based on the name of the class in the # inheritance hierarchy descending directly from ActiveRecord::Base. So if the hierarchy diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 2eefba2dfe..86eef9ca36 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -567,7 +567,7 @@ Joining, Preloading and eager loading of these associations is deprecated and wi end def derive_join_table - [active_record.table_name, klass.table_name].sort.join("\0").gsub(/^(.*_)(.+)\0\1(.+)/, '\1\2_\3').gsub("\0", "_") + ModelSchema.derive_join_table_name active_record.table_name, klass.table_name end def primary_key(klass) |