mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Deprecate set_table_name in favour of self.table_name= or defining your own method.
This commit is contained in:
parent
f73f53455a
commit
0b72a04d0c
21 changed files with 138 additions and 72 deletions
|
@ -2,36 +2,36 @@ require 'active_record_unit'
|
||||||
require 'fixtures/project'
|
require 'fixtures/project'
|
||||||
|
|
||||||
class Task < ActiveRecord::Base
|
class Task < ActiveRecord::Base
|
||||||
set_table_name 'projects'
|
self.table_name = 'projects'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Step < ActiveRecord::Base
|
class Step < ActiveRecord::Base
|
||||||
set_table_name 'projects'
|
self.table_name = 'projects'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Bid < ActiveRecord::Base
|
class Bid < ActiveRecord::Base
|
||||||
set_table_name 'projects'
|
self.table_name = 'projects'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Tax < ActiveRecord::Base
|
class Tax < ActiveRecord::Base
|
||||||
set_table_name 'projects'
|
self.table_name = 'projects'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Fax < ActiveRecord::Base
|
class Fax < ActiveRecord::Base
|
||||||
set_table_name 'projects'
|
self.table_name = 'projects'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Series < ActiveRecord::Base
|
class Series < ActiveRecord::Base
|
||||||
set_table_name 'projects'
|
self.table_name = 'projects'
|
||||||
end
|
end
|
||||||
|
|
||||||
module Blog
|
module Blog
|
||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
set_table_name 'projects'
|
self.table_name = 'projects'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Blog < ActiveRecord::Base
|
class Blog < ActiveRecord::Base
|
||||||
set_table_name 'projects'
|
self.table_name = 'projects'
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.use_relative_model_naming?
|
def self.use_relative_model_naming?
|
||||||
|
|
2
actionpack/test/fixtures/developer.rb
vendored
2
actionpack/test/fixtures/developer.rb
vendored
|
@ -5,5 +5,5 @@ class Developer < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class DeVeLoPeR < ActiveRecord::Base
|
class DeVeLoPeR < ActiveRecord::Base
|
||||||
set_table_name "developers"
|
self.table_name = "developers"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,21 @@
|
||||||
## Rails 3.2.0 (unreleased) ##
|
## Rails 3.2.0 (unreleased) ##
|
||||||
|
|
||||||
|
* Deprecated `set_table_name`. Use `self.table_name=` instead, or define your own
|
||||||
|
`self.table_name` method:
|
||||||
|
|
||||||
|
class Project < ActiveRecord::Base
|
||||||
|
self.table_name = "project"
|
||||||
|
end
|
||||||
|
|
||||||
|
class Post < ActiveRecord::Base
|
||||||
|
def self.table_name
|
||||||
|
"special_" + super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
Post.table_name # => "special_posts"
|
||||||
|
|
||||||
|
*Jon Leighton*
|
||||||
|
|
||||||
* Generated association methods are created within a separate module to allow overriding and
|
* Generated association methods are created within a separate module to allow overriding and
|
||||||
composition using `super`. For a class named `MyModel`, the module is named
|
composition using `super`. For a class named `MyModel`, the module is named
|
||||||
`MyModel::GeneratedFeatureMethods`. It is included into the model class immediately after
|
`MyModel::GeneratedFeatureMethods`. It is included into the model class immediately after
|
||||||
|
|
|
@ -23,6 +23,7 @@ require 'active_support/core_ext/module/delegation'
|
||||||
require 'active_support/core_ext/module/introspection'
|
require 'active_support/core_ext/module/introspection'
|
||||||
require 'active_support/core_ext/object/duplicable'
|
require 'active_support/core_ext/object/duplicable'
|
||||||
require 'active_support/core_ext/object/blank'
|
require 'active_support/core_ext/object/blank'
|
||||||
|
require 'active_support/deprecation'
|
||||||
require 'arel'
|
require 'arel'
|
||||||
require 'active_record/errors'
|
require 'active_record/errors'
|
||||||
require 'active_record/log_subscriber'
|
require 'active_record/log_subscriber'
|
||||||
|
@ -624,14 +625,61 @@ module ActiveRecord #:nodoc:
|
||||||
# the table name guess for an Invoice class becomes "myapp_invoices".
|
# the table name guess for an Invoice class becomes "myapp_invoices".
|
||||||
# Invoice::Lineitem becomes "myapp_invoice_lineitems".
|
# Invoice::Lineitem becomes "myapp_invoice_lineitems".
|
||||||
#
|
#
|
||||||
# You can also overwrite this class method to allow for unguessable
|
# You can also set your own table name explicitly:
|
||||||
# links, such as a Mouse class with a link to a "mice" table. Example:
|
|
||||||
#
|
#
|
||||||
# class Mouse < ActiveRecord::Base
|
# class Mouse < ActiveRecord::Base
|
||||||
# set_table_name "mice"
|
# self.table_name = "mice"
|
||||||
# end
|
# end
|
||||||
|
#
|
||||||
|
# Alternatively, you can override the table_name method to define your
|
||||||
|
# own computation. (Possibly using <tt>super</tt> to manipulate the default
|
||||||
|
# table name.) Example:
|
||||||
|
#
|
||||||
|
# class Post < ActiveRecord::Base
|
||||||
|
# def self.table_name
|
||||||
|
# "special_" + super
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
# Post.table_name # => "special_posts"
|
||||||
def table_name
|
def table_name
|
||||||
reset_table_name
|
reset_table_name unless defined?(@table_name)
|
||||||
|
@table_name
|
||||||
|
end
|
||||||
|
|
||||||
|
# Sets the table name explicitly. Example:
|
||||||
|
#
|
||||||
|
# class Project < ActiveRecord::Base
|
||||||
|
# self.table_name = "project"
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# You can also just define your own <tt>self.table_name</tt> method; see
|
||||||
|
# the documentation for ActiveRecord::Base#table_name.
|
||||||
|
def table_name=(value)
|
||||||
|
@quoted_table_name = nil
|
||||||
|
@arel_table = nil
|
||||||
|
@table_name = value
|
||||||
|
@relation = Relation.new(self, arel_table)
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_table_name(value = nil, &block) #:nodoc:
|
||||||
|
if block
|
||||||
|
ActiveSupport::Deprecation.warn(
|
||||||
|
"Calling set_table_name is deprecated. If you need to lazily evaluate " \
|
||||||
|
"the table name, define your own `self.table_name` class method. You can use `super` " \
|
||||||
|
"to get the default table name where you would have called `original_table_name`."
|
||||||
|
)
|
||||||
|
|
||||||
|
@quoted_table_name = nil
|
||||||
|
define_attr_method :table_name, value, &block
|
||||||
|
@arel_table = nil
|
||||||
|
@relation = Relation.new(self, arel_table)
|
||||||
|
else
|
||||||
|
ActiveSupport::Deprecation.warn(
|
||||||
|
"Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead."
|
||||||
|
)
|
||||||
|
|
||||||
|
self.table_name = value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a quoted version of the table name, used to construct SQL statements.
|
# Returns a quoted version of the table name, used to construct SQL statements.
|
||||||
|
@ -641,10 +689,14 @@ module ActiveRecord #:nodoc:
|
||||||
|
|
||||||
# Computes the table name, (re)sets it internally, and returns it.
|
# Computes the table name, (re)sets it internally, and returns it.
|
||||||
def reset_table_name #:nodoc:
|
def reset_table_name #:nodoc:
|
||||||
return if abstract_class?
|
if superclass.abstract_class?
|
||||||
|
self.table_name = superclass.table_name || compute_table_name
|
||||||
|
elsif abstract_class?
|
||||||
|
self.table_name = superclass == Base ? nil : superclass.table_name
|
||||||
|
else
|
||||||
self.table_name = compute_table_name
|
self.table_name = compute_table_name
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def full_table_name_prefix #:nodoc:
|
def full_table_name_prefix #:nodoc:
|
||||||
(parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix
|
(parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix
|
||||||
|
@ -668,21 +720,6 @@ module ActiveRecord #:nodoc:
|
||||||
default
|
default
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sets the table name. If the value is nil or false then the value returned by the given
|
|
||||||
# block is used.
|
|
||||||
#
|
|
||||||
# class Project < ActiveRecord::Base
|
|
||||||
# set_table_name "project"
|
|
||||||
# end
|
|
||||||
def set_table_name(value = nil, &block)
|
|
||||||
@quoted_table_name = nil
|
|
||||||
define_attr_method :table_name, value, &block
|
|
||||||
@arel_table = nil
|
|
||||||
|
|
||||||
@relation = Relation.new(self, arel_table)
|
|
||||||
end
|
|
||||||
alias :table_name= :set_table_name
|
|
||||||
|
|
||||||
# Sets the name of the inheritance column to use to the given value,
|
# Sets the name of the inheritance column to use to the given value,
|
||||||
# or (if the value # is nil or false) to the value returned by the
|
# or (if the value # is nil or false) to the value returned by the
|
||||||
# given block.
|
# given block.
|
||||||
|
|
|
@ -14,7 +14,7 @@ module ActiveRecord
|
||||||
@db_name = db
|
@db_name = db
|
||||||
|
|
||||||
@omgpost = Class.new(ActiveRecord::Base) do
|
@omgpost = Class.new(ActiveRecord::Base) do
|
||||||
set_table_name "#{db}.#{table}"
|
self.table_name = "#{db}.#{table}"
|
||||||
def self.name; 'Post'; end
|
def self.name; 'Post'; end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,7 +14,7 @@ module ActiveRecord
|
||||||
@db_name = db
|
@db_name = db
|
||||||
|
|
||||||
@omgpost = Class.new(ActiveRecord::Base) do
|
@omgpost = Class.new(ActiveRecord::Base) do
|
||||||
set_table_name "#{db}.#{table}"
|
self.table_name = "#{db}.#{table}"
|
||||||
def self.name; 'Post'; end
|
def self.name; 'Post'; end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,23 +26,23 @@ class SchemaTest < ActiveRecord::TestCase
|
||||||
PK_TABLE_NAME = 'table_with_pk'
|
PK_TABLE_NAME = 'table_with_pk'
|
||||||
|
|
||||||
class Thing1 < ActiveRecord::Base
|
class Thing1 < ActiveRecord::Base
|
||||||
set_table_name "test_schema.things"
|
self.table_name = "test_schema.things"
|
||||||
end
|
end
|
||||||
|
|
||||||
class Thing2 < ActiveRecord::Base
|
class Thing2 < ActiveRecord::Base
|
||||||
set_table_name "test_schema2.things"
|
self.table_name = "test_schema2.things"
|
||||||
end
|
end
|
||||||
|
|
||||||
class Thing3 < ActiveRecord::Base
|
class Thing3 < ActiveRecord::Base
|
||||||
set_table_name 'test_schema."things.table"'
|
self.table_name = 'test_schema."things.table"'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Thing4 < ActiveRecord::Base
|
class Thing4 < ActiveRecord::Base
|
||||||
set_table_name 'test_schema."Things"'
|
self.table_name = 'test_schema."Things"'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Thing5 < ActiveRecord::Base
|
class Thing5 < ActiveRecord::Base
|
||||||
set_table_name 'things'
|
self.table_name = 'things'
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
|
|
@ -14,7 +14,7 @@ class ViewTest < ActiveRecord::TestCase
|
||||||
]
|
]
|
||||||
|
|
||||||
class ThingView < ActiveRecord::Base
|
class ThingView < ActiveRecord::Base
|
||||||
set_table_name 'test_schema.view_things'
|
self.table_name = 'test_schema.view_things'
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
|
|
@ -4,7 +4,7 @@ require 'models/tagging'
|
||||||
|
|
||||||
module Namespaced
|
module Namespaced
|
||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
set_table_name 'posts'
|
self.table_name = 'posts'
|
||||||
has_one :tagging, :as => :taggable, :class_name => 'Tagging'
|
has_one :tagging, :as => :taggable, :class_name => 'Tagging'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,7 +23,7 @@ require 'models/treaty'
|
||||||
require 'active_support/core_ext/string/conversions'
|
require 'active_support/core_ext/string/conversions'
|
||||||
|
|
||||||
class ProjectWithAfterCreateHook < ActiveRecord::Base
|
class ProjectWithAfterCreateHook < ActiveRecord::Base
|
||||||
set_table_name 'projects'
|
self.table_name = 'projects'
|
||||||
has_and_belongs_to_many :developers,
|
has_and_belongs_to_many :developers,
|
||||||
:class_name => "DeveloperForProjectWithAfterCreateHook",
|
:class_name => "DeveloperForProjectWithAfterCreateHook",
|
||||||
:join_table => "developers_projects",
|
:join_table => "developers_projects",
|
||||||
|
@ -39,7 +39,7 @@ class ProjectWithAfterCreateHook < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
|
class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
|
||||||
set_table_name 'developers'
|
self.table_name = 'developers'
|
||||||
has_and_belongs_to_many :projects,
|
has_and_belongs_to_many :projects,
|
||||||
:class_name => "ProjectWithAfterCreateHook",
|
:class_name => "ProjectWithAfterCreateHook",
|
||||||
:join_table => "developers_projects",
|
:join_table => "developers_projects",
|
||||||
|
@ -48,7 +48,7 @@ class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class ProjectWithSymbolsForKeys < ActiveRecord::Base
|
class ProjectWithSymbolsForKeys < ActiveRecord::Base
|
||||||
set_table_name 'projects'
|
self.table_name = 'projects'
|
||||||
has_and_belongs_to_many :developers,
|
has_and_belongs_to_many :developers,
|
||||||
:class_name => "DeveloperWithSymbolsForKeys",
|
:class_name => "DeveloperWithSymbolsForKeys",
|
||||||
:join_table => :developers_projects,
|
:join_table => :developers_projects,
|
||||||
|
@ -57,7 +57,7 @@ class ProjectWithSymbolsForKeys < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class DeveloperWithSymbolsForKeys < ActiveRecord::Base
|
class DeveloperWithSymbolsForKeys < ActiveRecord::Base
|
||||||
set_table_name 'developers'
|
self.table_name = 'developers'
|
||||||
has_and_belongs_to_many :projects,
|
has_and_belongs_to_many :projects,
|
||||||
:class_name => "ProjectWithSymbolsForKeys",
|
:class_name => "ProjectWithSymbolsForKeys",
|
||||||
:join_table => :developers_projects,
|
:join_table => :developers_projects,
|
||||||
|
@ -66,7 +66,7 @@ class DeveloperWithSymbolsForKeys < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class DeveloperWithCounterSQL < ActiveRecord::Base
|
class DeveloperWithCounterSQL < ActiveRecord::Base
|
||||||
set_table_name 'developers'
|
self.table_name = 'developers'
|
||||||
has_and_belongs_to_many :projects,
|
has_and_belongs_to_many :projects,
|
||||||
:class_name => "DeveloperWithCounterSQL",
|
:class_name => "DeveloperWithCounterSQL",
|
||||||
:join_table => "developers_projects",
|
:join_table => "developers_projects",
|
||||||
|
|
|
@ -733,7 +733,7 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
|
||||||
class_name = "PostWith#{association.to_s.classify}#{dependency.to_s.classify}"
|
class_name = "PostWith#{association.to_s.classify}#{dependency.to_s.classify}"
|
||||||
Post.find(post_id).update_column :type, class_name
|
Post.find(post_id).update_column :type, class_name
|
||||||
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
|
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
|
||||||
klass.set_table_name 'posts'
|
klass.table_name = 'posts'
|
||||||
klass.send(association, association_name, :as => :taggable, :dependent => dependency)
|
klass.send(association, association_name, :as => :taggable, :dependent => dependency)
|
||||||
klass.find(post_id)
|
klass.find(post_id)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1431,16 +1431,19 @@ class BasicsTest < ActiveRecord::TestCase
|
||||||
k = Class.new( ActiveRecord::Base )
|
k = Class.new( ActiveRecord::Base )
|
||||||
k.table_name = "foo"
|
k.table_name = "foo"
|
||||||
assert_equal "foo", k.table_name
|
assert_equal "foo", k.table_name
|
||||||
|
|
||||||
|
assert_deprecated do
|
||||||
k.set_table_name "bar"
|
k.set_table_name "bar"
|
||||||
|
end
|
||||||
assert_equal "bar", k.table_name
|
assert_equal "bar", k.table_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_switching_between_table_name
|
def test_switching_between_table_name
|
||||||
assert_difference("GoodJoke.count") do
|
assert_difference("GoodJoke.count") do
|
||||||
Joke.set_table_name "cold_jokes"
|
Joke.table_name = "cold_jokes"
|
||||||
Joke.create
|
Joke.create
|
||||||
|
|
||||||
Joke.set_table_name "funny_jokes"
|
Joke.table_name = "funny_jokes"
|
||||||
Joke.create
|
Joke.create
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1448,19 +1451,29 @@ class BasicsTest < ActiveRecord::TestCase
|
||||||
def test_quoted_table_name_after_set_table_name
|
def test_quoted_table_name_after_set_table_name
|
||||||
klass = Class.new(ActiveRecord::Base)
|
klass = Class.new(ActiveRecord::Base)
|
||||||
|
|
||||||
klass.set_table_name "foo"
|
klass.table_name = "foo"
|
||||||
assert_equal "foo", klass.table_name
|
assert_equal "foo", klass.table_name
|
||||||
assert_equal klass.connection.quote_table_name("foo"), klass.quoted_table_name
|
assert_equal klass.connection.quote_table_name("foo"), klass.quoted_table_name
|
||||||
|
|
||||||
klass.set_table_name "bar"
|
klass.table_name = "bar"
|
||||||
assert_equal "bar", klass.table_name
|
assert_equal "bar", klass.table_name
|
||||||
assert_equal klass.connection.quote_table_name("bar"), klass.quoted_table_name
|
assert_equal klass.connection.quote_table_name("bar"), klass.quoted_table_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_set_table_name_with_block
|
def test_set_table_name_with_block
|
||||||
k = Class.new( ActiveRecord::Base )
|
k = Class.new( ActiveRecord::Base )
|
||||||
k.set_table_name { "ks" }
|
assert_deprecated do
|
||||||
assert_equal "ks", k.table_name
|
k.set_table_name "foo"
|
||||||
|
k.set_table_name { original_table_name + "ks" }
|
||||||
|
end
|
||||||
|
assert_equal "fooks", k.table_name
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_set_table_name_with_inheritance
|
||||||
|
k = Class.new( ActiveRecord::Base )
|
||||||
|
def k.name; "Foo"; end
|
||||||
|
def k.table_name; super + "ks"; end
|
||||||
|
assert_equal "foosks", k.table_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_set_primary_key_with_value
|
def test_set_primary_key_with_value
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
require "cases/helper"
|
require "cases/helper"
|
||||||
|
|
||||||
class CallbackDeveloper < ActiveRecord::Base
|
class CallbackDeveloper < ActiveRecord::Base
|
||||||
set_table_name 'developers'
|
self.table_name = 'developers'
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def callback_string(callback_method)
|
def callback_string(callback_method)
|
||||||
|
@ -48,7 +48,7 @@ class CallbackDeveloperWithFalseValidation < CallbackDeveloper
|
||||||
end
|
end
|
||||||
|
|
||||||
class ParentDeveloper < ActiveRecord::Base
|
class ParentDeveloper < ActiveRecord::Base
|
||||||
set_table_name 'developers'
|
self.table_name = 'developers'
|
||||||
attr_accessor :after_save_called
|
attr_accessor :after_save_called
|
||||||
before_validation {|record| record.after_save_called = true}
|
before_validation {|record| record.after_save_called = true}
|
||||||
end
|
end
|
||||||
|
@ -58,7 +58,7 @@ class ChildDeveloper < ParentDeveloper
|
||||||
end
|
end
|
||||||
|
|
||||||
class RecursiveCallbackDeveloper < ActiveRecord::Base
|
class RecursiveCallbackDeveloper < ActiveRecord::Base
|
||||||
set_table_name 'developers'
|
self.table_name = 'developers'
|
||||||
|
|
||||||
before_save :on_before_save
|
before_save :on_before_save
|
||||||
after_save :on_after_save
|
after_save :on_after_save
|
||||||
|
@ -79,7 +79,7 @@ class RecursiveCallbackDeveloper < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class ImmutableDeveloper < ActiveRecord::Base
|
class ImmutableDeveloper < ActiveRecord::Base
|
||||||
set_table_name 'developers'
|
self.table_name = 'developers'
|
||||||
|
|
||||||
validates_inclusion_of :salary, :in => 50000..200000
|
validates_inclusion_of :salary, :in => 50000..200000
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ class ImmutableDeveloper < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class ImmutableMethodDeveloper < ActiveRecord::Base
|
class ImmutableMethodDeveloper < ActiveRecord::Base
|
||||||
set_table_name 'developers'
|
self.table_name = 'developers'
|
||||||
|
|
||||||
validates_inclusion_of :salary, :in => 50000..200000
|
validates_inclusion_of :salary, :in => 50000..200000
|
||||||
|
|
||||||
|
@ -118,7 +118,7 @@ class ImmutableMethodDeveloper < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class OnCallbacksDeveloper < ActiveRecord::Base
|
class OnCallbacksDeveloper < ActiveRecord::Base
|
||||||
set_table_name 'developers'
|
self.table_name = 'developers'
|
||||||
|
|
||||||
before_validation { history << :before_validation }
|
before_validation { history << :before_validation }
|
||||||
before_validation(:on => :create){ history << :before_validation_on_create }
|
before_validation(:on => :create){ history << :before_validation_on_create }
|
||||||
|
@ -138,7 +138,7 @@ class OnCallbacksDeveloper < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class CallbackCancellationDeveloper < ActiveRecord::Base
|
class CallbackCancellationDeveloper < ActiveRecord::Base
|
||||||
set_table_name 'developers'
|
self.table_name = 'developers'
|
||||||
|
|
||||||
attr_reader :after_save_called, :after_create_called, :after_update_called, :after_destroy_called
|
attr_reader :after_save_called, :after_create_called, :after_update_called, :after_destroy_called
|
||||||
attr_accessor :cancel_before_save, :cancel_before_create, :cancel_before_update, :cancel_before_destroy
|
attr_accessor :cancel_before_save, :cancel_before_create, :cancel_before_update, :cancel_before_destroy
|
||||||
|
|
|
@ -10,7 +10,7 @@ require 'models/string_key_object'
|
||||||
class LockWithoutDefault < ActiveRecord::Base; end
|
class LockWithoutDefault < ActiveRecord::Base; end
|
||||||
|
|
||||||
class LockWithCustomColumnWithoutDefault < ActiveRecord::Base
|
class LockWithCustomColumnWithoutDefault < ActiveRecord::Base
|
||||||
set_table_name :lock_without_defaults_cust
|
self.table_name = :lock_without_defaults_cust
|
||||||
set_locking_column :custom_lock_version
|
set_locking_column :custom_lock_version
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1027,7 +1027,7 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||||
t.column :title, :string
|
t.column :title, :string
|
||||||
end
|
end
|
||||||
person_klass = Class.new(Person)
|
person_klass = Class.new(Person)
|
||||||
person_klass.set_table_name 'testings'
|
person_klass.table_name = 'testings'
|
||||||
|
|
||||||
person_klass.connection.add_column "testings", "wealth", :integer, :null => false, :default => 99
|
person_klass.connection.add_column "testings", "wealth", :integer, :null => false, :default => 99
|
||||||
person_klass.reset_column_information
|
person_klass.reset_column_information
|
||||||
|
|
|
@ -6,7 +6,7 @@ class TransactionCallbacksTest < ActiveRecord::TestCase
|
||||||
fixtures :topics
|
fixtures :topics
|
||||||
|
|
||||||
class TopicWithCallbacks < ActiveRecord::Base
|
class TopicWithCallbacks < ActiveRecord::Base
|
||||||
set_table_name :topics
|
self.table_name = :topics
|
||||||
|
|
||||||
after_commit{|record| record.send(:do_after_commit, nil)}
|
after_commit{|record| record.send(:do_after_commit, nil)}
|
||||||
after_commit(:on => :create){|record| record.send(:do_after_commit, :create)}
|
after_commit(:on => :create){|record| record.send(:do_after_commit, :create)}
|
||||||
|
@ -252,7 +252,7 @@ class TransactionObserverCallbacksTest < ActiveRecord::TestCase
|
||||||
fixtures :topics
|
fixtures :topics
|
||||||
|
|
||||||
class TopicWithObserverAttached < ActiveRecord::Base
|
class TopicWithObserverAttached < ActiveRecord::Base
|
||||||
set_table_name :topics
|
self.table_name = :topics
|
||||||
def history
|
def history
|
||||||
@history ||= []
|
@history ||= []
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ require 'models/parrot'
|
||||||
require 'models/company'
|
require 'models/company'
|
||||||
|
|
||||||
class ProtectedPerson < ActiveRecord::Base
|
class ProtectedPerson < ActiveRecord::Base
|
||||||
set_table_name 'people'
|
self.table_name = 'people'
|
||||||
attr_accessor :addon
|
attr_accessor :addon
|
||||||
attr_protected :first_name
|
attr_protected :first_name
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class Joke < ActiveRecord::Base
|
class Joke < ActiveRecord::Base
|
||||||
set_table_name 'funny_jokes'
|
self.table_name = 'funny_jokes'
|
||||||
end
|
end
|
||||||
|
|
||||||
class GoodJoke < ActiveRecord::Base
|
class GoodJoke < ActiveRecord::Base
|
||||||
set_table_name 'funny_jokes'
|
self.table_name = 'funny_jokes'
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
class Liquid < ActiveRecord::Base
|
class Liquid < ActiveRecord::Base
|
||||||
set_table_name :liquid
|
self.table_name = :liquid
|
||||||
has_many :molecules, :uniq => true
|
has_many :molecules, :uniq => true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
class WarehouseThing < ActiveRecord::Base
|
class WarehouseThing < ActiveRecord::Base
|
||||||
set_table_name "warehouse-things"
|
self.table_name = "warehouse-things"
|
||||||
|
|
||||||
validates_uniqueness_of :value
|
validates_uniqueness_of :value
|
||||||
end
|
end
|
|
@ -101,11 +101,11 @@ h3. Overriding the Naming Conventions
|
||||||
|
|
||||||
What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions.
|
What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions.
|
||||||
|
|
||||||
You can use the +ActiveRecord::Base.set_table_name+ method to specify the table name that should be used:
|
You can use the +ActiveRecord::Base.table_name=+ method to specify the table name that should be used:
|
||||||
|
|
||||||
<ruby>
|
<ruby>
|
||||||
class Product < ActiveRecord::Base
|
class Product < ActiveRecord::Base
|
||||||
set_table_name "PRODUCT"
|
self.table_name = "PRODUCT"
|
||||||
end
|
end
|
||||||
</ruby>
|
</ruby>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue