aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2015-12-02 16:57:29 +0100
committerYves Senn <yves.senn@gmail.com>2015-12-02 16:57:29 +0100
commit4294a7eebf52759be61ed57908436d6324f26120 (patch)
tree352d5c7ab78912474a72eb7d5dfaa8151072cd91 /activerecord/lib
parent0f82f661b75a4cdeae027e80940200061b0496c2 (diff)
downloadrails-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')
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb12
-rw-r--r--activerecord/lib/active_record/inheritance.rb4
2 files changed, 14 insertions, 2 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.
diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb
index 4ae9920efb..a46afed7f4 100644
--- a/activerecord/lib/active_record/inheritance.rb
+++ b/activerecord/lib/active_record/inheritance.rb
@@ -51,7 +51,7 @@ module ActiveRecord
end
attrs = args.first
- if attribute_names.include?(inheritance_column)
+ if has_attribute?(inheritance_column)
subclass = subclass_from_attributes(attrs) || subclass_from_defaults
end
@@ -163,7 +163,7 @@ module ActiveRecord
end
def using_single_table_inheritance?(record)
- record[inheritance_column].present? && columns_hash.include?(inheritance_column)
+ record[inheritance_column].present? && has_attribute?(inheritance_column)
end
def find_sti_class(type_name)