From 7a3d76b3cbd5d4705bb850b434243215db3ad688 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Sun, 29 Jun 2014 12:51:42 -0600 Subject: Don't encourage usage of `columns_hash` As discussed in https://github.com/plataformatec/simple_form/pull/1094, we should not encourage usage of `columns_hash`, and instead provide an alternate method to determine whether or not an attribute exists. The language `attribute` was chosen over `column` since these are in the `AttributeMethods` module. --- activerecord/lib/active_record/attribute_methods.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 51f6a009db..75154a7975 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -202,12 +202,17 @@ module ActiveRecord if column.nil? ActiveSupport::Deprecation.warn(<<-MESSAGE.strip_heredoc) `column_for_attribute` will return a null object for non-existent columns - in Rails 5.0. If you would like to continue to receive `nil`, you should - instead call `model.class.columns_hash[name]` + in Rails 5.0. Use `attribute_exists?` if you need to check for an + attribute's existence. MESSAGE end column end + + # Returns whether or not an attribute exists with the given name. + def attribute_exists?(name) + @attributes.include?(name) + end end # A Person object with a name attribute can ask person.respond_to?(:name), -- cgit v1.2.3