aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-04 06:26:03 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-04 06:26:03 -0600
commite8375a662bc47647f4bd39779ada018cc634fe97 (patch)
treea608f3df3c8d652d5749ee78cc8d74eea972aa02 /activerecord/lib/active_record/attribute_methods.rb
parent0329d59a65a5afbf57de83670e3e05e4a73815e4 (diff)
downloadrails-e8375a662bc47647f4bd39779ada018cc634fe97.tar.gz
rails-e8375a662bc47647f4bd39779ada018cc634fe97.tar.bz2
rails-e8375a662bc47647f4bd39779ada018cc634fe97.zip
Use null column for association key types
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods.rb')
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb40
1 files changed, 21 insertions, 19 deletions
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.