method
connection
v2.1.0 -
Show latest stable
- Class:
ActiveRecord::Base
connection()public
Returns the connection currently associated with the class. This can also be used to "borrow" the connection to do database work unrelated to any of the specific Active Records.
# File activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb, line 73
def connection
if defined?(@active_connection_name) && (conn = active_connections[@active_connection_name])
conn
else
# retrieve_connection sets the cache key.
conn = retrieve_connection
active_connections[@active_connection_name] = conn
end
end 1Note
Current Database Name
cracell ยท Aug 19, 20083 thanksThe MySQL database adapter extends this and allows you to call
ActiveRecord::Base.connection.current_database
to get the current databases name. Useful when you are actively changing the database you are connected to and sometimes need to check the current one.
http://apidock.com/rails/ActiveRecord/ConnectionAdapters/MysqlAdapter/current_database
Related methods
- Instance methods
- ==
- []
- []=
- attribute_for_inspect
- attribute_names
- attribute_present?
- attributes
- attributes=
- attributes_before_type_cast
- becomes
- cache_key
- clone
- column_for_attribute
- connection
- decrement
- decrement!
- destroy
- eql?
- freeze
- frozen?
- has_attribute?
- hash
- id
- id=
- id_before_type_cast
- increment
- increment!
- inspect
- new_record?
- quoted_id
- readonly!
- readonly?
- reload
- save
- save!
- to_param
- toggle
- toggle!
- update_attribute
- update_attributes
- update_attributes!
- Class methods
- ===
- abstract_class?
- accessible_attributes
- active_connection_name
- all
- allow_concurrency=
- attr_accessible
- attr_protected
- attr_readonly
- base_class
- benchmark
- class_name
- clear_active_connection_name
- clear_active_connections!
- clear_reloadable_connections!
- column_methods_hash
- column_names
- columns
- columns_hash
- connected?
- connection
- connection=
- content_columns
- count_by_sql
- create
- decrement_counter
- delete
- delete_all
- descends_from_active_record?
- destroy
- destroy_all
- establish_connection
- exists?
- find
- find_by_sql
- finder_needs_type_condition?
- first
- get_primary_key
- human_attribute_name
- increment_counter
- inheritance_column
- inherited
- inspect
- last
- log_connections
- mysql_connection
- new
- postgresql_connection
- primary_key
- protected_attributes
- quote_value
- readonly_attributes
- remove_connection
- require_mysql
- reset_column_information
- reset_column_information_and_inheritable_attributes_for_all_subclasses
- reset_primary_key
- reset_sequence_name
- reset_subclasses
- reset_table_name
- respond_to?
- retrieve_connection
- sanitize
- sequence_name
- serialize
- serialized_attributes
- set_inheritance_column
- set_primary_key
- set_sequence_name
- set_table_name
- silence
- single_threaded_active_connections
- sqlite3_connection
- sqlite_connection
- sti_name
- table_exists?
- table_name
- thread_safe_active_connections
- update
- update_all
- update_counters
- verify_active_connections!
- Protected methods
-
aggregate_mapping -
class_name_of_active_record_descendant -
class_of_active_record_descendant -
compute_type -
current_scoped_methods -
encode_quoted_value -
expand_hash_conditions_for_aggregates -
expand_range_bind_variables -
quote_bound_value -
raise_if_bind_arity_mismatch -
replace_bind_variables -
replace_named_bind_variables -
sanitize_sql_array -
sanitize_sql_for_assignment -
sanitize_sql_for_conditions -
sanitize_sql_hash_for_assignment -
sanitize_sql_hash_for_conditions -
scope -
scoped? -
set_readonly_option! -
single_threaded_scoped_methods -
subclasses -
thread_safe_scoped_methods -
validate_find_options -
with_exclusive_scope -
with_scope - Private methods
-
add_conditions! -
add_group! -
add_joins! -
add_limit! -
add_lock! -
add_order! -
all_attributes_exists? -
attribute_condition -
clear_all_cached_connections! -
clear_cache! -
construct_attributes_from_arguments -
construct_finder_sql -
define_attr_method -
determine_finder -
determine_instantiator -
expand_attribute_names_for_aggregates -
expand_id_conditions -
extract_attribute_names_from_match -
find_every -
find_from_ids -
find_initial -
find_last -
find_one -
find_some -
instantiate -
matches_dynamic_finder? -
matches_dynamic_finder_with_initialize_or_create? -
merge_conditions -
merge_includes -
method_missing -
parse_sqlite_config! -
quoted_table_name -
remove_stale_cached_threads! -
reverse_sql_order -
safe_to_array -
type_condition -
type_name_with_module -
undecorated_table_name -
assign_multiparameter_attributes -
attributes_from_column_definition -
attributes_protected_by_default -
attributes_with_quotes -
clone_attribute_value -
clone_attributes -
comma_pair_list -
convert_number_column_value -
create -
create_or_update -
ensure_proper_type -
execute_callstack_for_multiparameter_attributes -
extract_callstack_for_multiparameter_attributes -
find_parameter_position -
instantiate_time_object -
interpolate_sql -
object_from_yaml -
quote_columns -
quote_value -
quoted_column_names -
quoted_comma_pair_list -
remove_attributes_protected_from_mass_assignment -
remove_readonly_attributes -
type_cast_attribute_value -
update