Skip to content
This repository
Browse code

make set_table_name take effect immediately

  • Loading branch information...
commit 9643243204fab063630380f42fcd4b8160044104 1 parent ac86923
Jan janx authored tenderlove committed
3  activerecord/lib/active_record/base.rb
@@ -636,6 +636,9 @@ def reset_sequence_name #:nodoc:
636 636 def set_table_name(value = nil, &block)
637 637 @quoted_table_name = nil
638 638 define_attr_method :table_name, value, &block
  639 +
  640 + @arel_table = Arel::Table.new(table_name, :engine => arel_engine)
  641 + @relation = Relation.new(self, arel_table)
639 642 end
640 643 alias :table_name= :set_table_name
641 644
11 activerecord/test/cases/base_test.rb
@@ -20,6 +20,7 @@
20 20 require 'models/parrot'
21 21 require 'models/loose_person'
22 22 require 'models/edge'
  23 +require 'models/joke'
23 24 require 'rexml/document'
24 25 require 'active_support/core_ext/exception'
25 26
@@ -1156,6 +1157,16 @@ def test_set_table_name_with_value
1156 1157 assert_equal "bar", k.table_name
1157 1158 end
1158 1159
  1160 + def test_switching_between_table_name
  1161 + assert_difference("GoodJoke.count") do
  1162 + Joke.set_table_name "cold_jokes"
  1163 + Joke.create
  1164 +
  1165 + Joke.set_table_name "funny_jokes"
  1166 + Joke.create
  1167 + end
  1168 + end
  1169 +
1159 1170 def test_quoted_table_name_after_set_table_name
1160 1171 klass = Class.new(ActiveRecord::Base)
1161 1172
4 activerecord/test/models/joke.rb
... ... @@ -1,3 +1,7 @@
1 1 class Joke < ActiveRecord::Base
2 2 set_table_name 'funny_jokes'
3 3 end
  4 +
  5 +class GoodJoke < ActiveRecord::Base
  6 + set_table_name 'funny_jokes'
  7 +end
4 activerecord/test/schema/schema.rb
@@ -229,6 +229,10 @@ def create_table(*args, &block)
229 229 t.string :name
230 230 end
231 231
  232 + create_table :cold_jokes, :force => true do |t|
  233 + t.string :name
  234 + end
  235 +
232 236 create_table :goofy_string_id, :force => true, :id => false do |t|
233 237 t.string :id, :null => false
234 238 t.string :info

0 comments on commit 9643243

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