aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/lib/active_record/base.rb3
-rw-r--r--activerecord/test/cases/base_test.rb11
-rw-r--r--activerecord/test/models/joke.rb4
-rw-r--r--activerecord/test/schema/schema.rb4
4 files changed, 22 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index effb17b2ff..2d5c166075 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -636,6 +636,9 @@ module ActiveRecord #:nodoc:
def set_table_name(value = nil, &block)
@quoted_table_name = nil
define_attr_method :table_name, value, &block
+
+ @arel_table = Arel::Table.new(table_name, :engine => arel_engine)
+ @relation = Relation.new(self, arel_table)
end
alias :table_name= :set_table_name
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 68adeff882..1fa5d2ac5f 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -20,6 +20,7 @@ require 'models/warehouse_thing'
require 'models/parrot'
require 'models/loose_person'
require 'models/edge'
+require 'models/joke'
require 'rexml/document'
require 'active_support/core_ext/exception'
@@ -1156,6 +1157,16 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal "bar", k.table_name
end
+ def test_switching_between_table_name
+ assert_difference("GoodJoke.count") do
+ Joke.set_table_name "cold_jokes"
+ Joke.create
+
+ Joke.set_table_name "funny_jokes"
+ Joke.create
+ end
+ end
+
def test_quoted_table_name_after_set_table_name
klass = Class.new(ActiveRecord::Base)
diff --git a/activerecord/test/models/joke.rb b/activerecord/test/models/joke.rb
index 3978abc2ba..d7f01e59e6 100644
--- a/activerecord/test/models/joke.rb
+++ b/activerecord/test/models/joke.rb
@@ -1,3 +1,7 @@
class Joke < ActiveRecord::Base
set_table_name 'funny_jokes'
end
+
+class GoodJoke < ActiveRecord::Base
+ set_table_name 'funny_jokes'
+end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 665a4fe914..b1763ff431 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -229,6 +229,10 @@ ActiveRecord::Schema.define do
t.string :name
end
+ create_table :cold_jokes, :force => true do |t|
+ t.string :name
+ end
+
create_table :goofy_string_id, :force => true, :id => false do |t|
t.string :id, :null => false
t.string :info