diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-12-02 16:57:29 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-12-02 16:57:29 +0100 |
commit | 4294a7eebf52759be61ed57908436d6324f26120 (patch) | |
tree | 352d5c7ab78912474a72eb7d5dfaa8151072cd91 /activerecord/lib/active_record/attribute_methods.rb | |
parent | 0f82f661b75a4cdeae027e80940200061b0496c2 (diff) | |
download | rails-4294a7eebf52759be61ed57908436d6324f26120.tar.gz rails-4294a7eebf52759be61ed57908436d6324f26120.tar.bz2 rails-4294a7eebf52759be61ed57908436d6324f26120.zip |
add `ActiveRecord::Base.has_attribute?`
`has_attribute?` method to check wether a given attribute has been defined.
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods.rb')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 4ae585d3f5..423a93964e 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -191,6 +191,18 @@ module ActiveRecord end end + # Returns true if the given attribute exists, otherwise false. + # + # class Person < ActiveRecord::Base + # end + # + # Person.has_attribute?('name') # => true + # Person.has_attribute?(:age) # => true + # Person.has_attribute?(:nothing) # => false + def has_attribute?(attr_name) + attribute_types.key?(attr_name.to_s) + end + # Returns the column object for the named attribute. # Returns a +ActiveRecord::ConnectionAdapters::NullColumn+ if the # named attribute does not exist. |