diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-06-04 22:02:25 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-06-04 22:02:25 +0200 |
commit | e2e308bbe7188dd59f8f92f8da5dde34c0f45c78 (patch) | |
tree | 98054fd288d510c58cfb09185d3f89824820e7c5 /activerecord | |
parent | 1ce0cc0d38c9f5058cb484612fab625db823c4e4 (diff) | |
parent | e8375a662bc47647f4bd39779ada018cc634fe97 (diff) | |
download | rails-e2e308bbe7188dd59f8f92f8da5dde34c0f45c78.tar.gz rails-e2e308bbe7188dd59f8f92f8da5dde34c0f45c78.tar.bz2 rails-e2e308bbe7188dd59f8f92f8da5dde34c0f45c78.zip |
Merge pull request #15502 from sgrif/sg-use-null-column
Use null column for association key types
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/association.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 40 |
2 files changed, 23 insertions, 23 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index 1b83700613..33c8619359 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -104,13 +104,11 @@ module ActiveRecord end def association_key_type - column = @klass.column_types[association_key_name.to_s] - column && column.type + @klass.column_for_attribute(association_key_name).type end def owner_key_type - column = @model.column_types[owner_key_name.to_s] - column && column.type + @model.column_for_attribute(owner_key_name).type end def load_slices(slices) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index e56a4cc805..dccbd48592 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -18,6 +18,8 @@ module ActiveRecord include TimeZoneConversion include Dirty include Serialization + + delegate :column_for_attribute, to: :class end AttrNames = Module.new { @@ -192,6 +194,25 @@ module ActiveRecord [] end end + + # Returns the column object for the named attribute. Returns +nil+ if the + # named attribute not exists. + # + # class Person < ActiveRecord::Base + # end + # + # person = Person.new + # person.column_for_attribute(:name) # the result depends on the ConnectionAdapter + # # => #<ActiveRecord::ConnectionAdapters::SQLite3Column:0x007ff4ab083980 @name="name", @sql_type="varchar(255)", @null=true, ...> + # + # person.column_for_attribute(:nothing) + # # => #<ActiveRecord::ConnectionAdapters::Column:0xXXX @name=nil, @sql_type=nil, @cast_type=#<Type::Value>, ...> + def column_for_attribute(name) + name = name.to_s + columns_hash.fetch(name) do + ConnectionAdapters::Column.new(name, nil, Type::Value.new) + end + end end # If we haven't generated any methods yet, generate them, then @@ -339,25 +360,6 @@ module ActiveRecord !value.nil? && !(value.respond_to?(:empty?) && value.empty?) end - # Returns the column object for the named attribute. Returns +nil+ if the - # named attribute not exists. - # - # class Person < ActiveRecord::Base - # end - # - # person = Person.new - # person.column_for_attribute(:name) # the result depends on the ConnectionAdapter - # # => #<ActiveRecord::ConnectionAdapters::SQLite3Column:0x007ff4ab083980 @name="name", @sql_type="varchar(255)", @null=true, ...> - # - # person.column_for_attribute(:nothing) - # # => #<ActiveRecord::ConnectionAdapters::Column:0xXXX @name=nil, @sql_type=nil, @cast_type=#<Type::Value>, ...> - def column_for_attribute(name) - name = name.to_s - self.class.columns_hash.fetch(name) do - ConnectionAdapters::Column.new(name, nil, Type::Value.new) - end - end - # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example, # "2004-12-12" in a date column is cast to a date object, like Date.new(2004, 12, 12)). It raises # <tt>ActiveModel::MissingAttributeError</tt> if the identified attribute is missing. |