method
initialize_copy
v3.0.0 -
Show latest stable
- Class:
ActiveRecord::Base
initialize_copy(other)public
Cloned objects have no id assigned and are treated as new records. Note that this is a "shallow" clone as it copies the object’s attributes only, not its associations. The extent of a "deep" clone is application specific and is therefore left to the application to implement according to its need.
# File activerecord/lib/active_record/base.rb, line 1423
def initialize_copy(other)
_run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
cloned_attributes.delete(self.class.primary_key)
@attributes = cloned_attributes
@changed_attributes = {}
attributes_from_column_definition.each do |attr, orig_value|
@changed_attributes[attr] = orig_value if field_changed?(attr, orig_value, @attributes[attr])
end
clear_aggregation_cache
clear_association_cache
@attributes_cache = {}
@new_record = true
ensure_proper_type
if scope = self.class.send(:current_scoped_methods)
create_with = scope.scope_for_create
create_with.each { |att,value| self.send("#{att}=", value) } if create_with
end
end Related methods
- Instance methods
- ==
- []
- []=
- attribute_for_inspect
- attribute_names
- attribute_present?
- attributes
- attributes=
- cache_key
- column_for_attribute
- connection
- dup
- eql?
- freeze
- frozen?
- has_attribute?
- hash
- initialize_copy
- inspect
- quoted_id
- readonly!
- readonly?
- to_param
- Class methods
- ===
- abstract_class?
- arel_engine
- arel_table
- attr_readonly
- attribute_method?
- base_class
- colorize_logging
- column_methods_hash
- column_names
- columns
- columns_hash
- connected?
- connection
- connection_pool
- content_columns
- count_by_sql
- create
- descends_from_active_record?
- establish_connection
- find_by_sql
- finder_needs_type_condition?
- full_table_name_prefix
- i18n_scope
- inheritance_column
- inspect
- lookup_ancestors
- mysql_connection
- new
- postgresql_connection
- quote_value
- quoted_table_name
- readonly_attributes
- remove_connection
- reset_column_information
- reset_column_information_and_inheritable_attributes_for_all_subclasses
- reset_sequence_name
- reset_subclasses
- reset_table_name
- respond_to?
- retrieve_connection
- sanitize
- scoped_methods
- sequence_name
- serialize
- serialized_attributes
- set_inheritance_column
- set_sequence_name
- set_table_name
- sqlite3_connection
- sti_name
- subclasses
- table_exists?
- table_name
- unscoped
- Protected methods
-
aggregate_mapping -
class_of_active_record_descendant -
compute_type -
current_scoped_methods -
default_scope -
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 -
with_exclusive_scope -
with_scope -
clone_attribute_value -
clone_attributes - Private methods
-
all_attributes_exists? -
attributes_protected_by_default -
compute_table_name -
construct_attributes_from_arguments -
construct_finder_arel -
expand_attribute_names_for_aggregates -
find_sti_class -
instantiate -
method_missing -
relation -
type_condition -
undecorated_table_name -
arel_attributes_values -
assign_multiparameter_attributes -
comma_pair_list -
convert_number_column_value -
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_comma_pair_list -
type_cast_attribute_value