diff options
Diffstat (limited to 'activerecord/lib/active_record/base.rb')
-rw-r--r-- | activerecord/lib/active_record/base.rb | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 0941700803..effb17b2ff 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -249,6 +249,17 @@ module ActiveRecord #:nodoc: # user = User.create(:preferences => %w( one two three )) # User.find(user.id).preferences # raises SerializationTypeMismatch # + # When you specify a class option, the default value for that attribute will be a new + # instance of that class. + # + # class User < ActiveRecord::Base + # serialize :preferences, OpenStruct + # end + # + # user = User.new + # user.preferences.theme_color = "red" + # + # # == Single table inheritance # # Active Record allows inheritance by storing the name of the class in a column that by @@ -668,16 +679,12 @@ module ActiveRecord #:nodoc: # Returns an array of column objects for the table associated with this class. def columns - @@columns[table_name] ||= connection.columns( - table_name, "#{name} Columns" - ).tap { |columns| - columns.each { |column| column.primary = column.name == primary_key } - } + connection_pool.columns[table_name] end # Returns a hash of column objects for the table associated with this class. def columns_hash - @@columns_hash[table_name] ||= Hash[columns.map { |column| [column.name, column] }] + connection_pool.columns_hash[table_name] end # Returns an array of column names as strings. @@ -734,14 +741,14 @@ module ActiveRecord #:nodoc: def reset_column_information connection.clear_cache! undefine_attribute_methods - reset_column_cache + connection_pool.clear_table_cache!(table_name) if table_exists? + @column_names = @content_columns = @dynamic_methods_hash = @inheritance_column = nil - @arel_engine = @relation = @arel_table = nil + @arel_engine = @relation = nil end - def reset_column_cache # :nodoc: - @@columns.delete table_name - @@columns_hash.delete table_name + def clear_cache! # :nodoc: + connection_pool.clear_cache! end def attribute_method?(attribute) @@ -840,7 +847,7 @@ module ActiveRecord #:nodoc: end def arel_table - @arel_table ||= Arel::Table.new(table_name, arel_engine) + Arel::Table.new(table_name, arel_engine) end def arel_engine @@ -1388,8 +1395,6 @@ MSG quoted_value end end - @@columns_hash = {} - @@columns = {} public # New objects can be instantiated as either empty (pass no construction parameter) or pre-set with @@ -1409,6 +1414,7 @@ MSG @changed_attributes = {} ensure_proper_type + set_serialized_attributes populate_with_current_scope_attributes self.attributes = attributes unless attributes.nil? @@ -1447,10 +1453,7 @@ MSG def init_with(coder) @attributes = coder['attributes'] - (@attributes.keys & self.class.serialized_attributes.keys).each do |key| - coder = self.class.serialized_attributes[key] - @attributes[key] = coder.load @attributes[key] - end + set_serialized_attributes @attributes_cache, @previously_changed, @changed_attributes = {}, {}, {} @association_cache = {} @@ -1724,6 +1727,13 @@ MSG private + def set_serialized_attributes + (@attributes.keys & self.class.serialized_attributes.keys).each do |key| + coder = self.class.serialized_attributes[key] + @attributes[key] = coder.load @attributes[key] + end + end + # Sets the attribute used for single table inheritance to this class name if this is not the # ActiveRecord::Base descendant. # Considering the hierarchy Reply < Message < ActiveRecord::Base, this makes it possible to |