Skip to content
Browse files

Merge pull request #14965 from eric-chahin/issue_14824

Fixed the inferred table name of a HABTM auxiliar

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb
  • Loading branch information...
1 parent bb39fff commit 9c3c55970af1149ee1430f872481ec7436b6d629 @matthewd matthewd committed May 20, 2014
View
6 activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Fixed the inferred table name of a HABTM auxiliar table inside a schema.
+
+ Fixes #14824
+
+ *Eric Chahin*
+
* Fix bug that added `table_name_prefix` and `table_name_suffix` to
extension names in PostgreSQL when migrating.
View
2 activerecord/lib/active_record/reflection.rb
@@ -384,7 +384,7 @@ def derive_foreign_key
end
def derive_join_table
- [active_record.table_name, klass.table_name].sort.join("\0").gsub(/^(.*_)(.+)\0\1(.+)/, '\1\2_\3').gsub("\0", "_")
+ [active_record.table_name, klass.table_name].sort.join("\0").gsub(/^(.*[._])(.+)\0\1(.+)/, '\1\2_\3').gsub("\0", "_")
end
def primary_key(klass)
View
26 activerecord/test/cases/adapters/postgresql/schema_test.rb
@@ -50,6 +50,16 @@ class Thing5 < ActiveRecord::Base
self.table_name = 'things'
end
+ class Song < ActiveRecord::Base
+ self.table_name = "music.songs"
+ has_and_belongs_to_many :albums
+ end
+
+ class Album < ActiveRecord::Base
+ self.table_name = "music.albums"
+ has_and_belongs_to_many :songs
+ end
+
def setup
@connection = ActiveRecord::Base.connection
@connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
@@ -109,6 +119,22 @@ def test_drop_schema
assert !@connection.schema_names.include?("test_schema3")
end
+ def test_habtm_table_name_with_schema
+ ActiveRecord::Base.connection.execute <<-SQL
+ DROP SCHEMA IF EXISTS music CASCADE;
+ CREATE SCHEMA music;
+ CREATE TABLE music.albums (id serial primary key);
+ CREATE TABLE music.songs (id serial primary key);
+ CREATE TABLE music.albums_songs (album_id integer, song_id integer);
+ SQL
+
+ song = Song.create
+ album = Album.create
+ assert_equal song, Song.includes(:albums).references(:albums).first
+ ensure
+ ActiveRecord::Base.connection.execute "DROP SCHEMA music CASCADE;"
+ end
+
def test_raise_drop_schema_with_nonexisting_schema
assert_raises(ActiveRecord::StatementInvalid) do
@connection.drop_schema "test_schema3"

0 comments on commit 9c3c559

Please sign in to comment.
Something went wrong with that request. Please try again.