aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-11 00:45:26 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-11 00:45:26 +0000
commitb067bf788454106de761ad53af460be9fde48abd (patch)
tree941f7821900ba8288b08686b63016c2329036337 /activerecord/lib/active_record
parent18e945d8872517654e087e646890bf2f8f31bc03 (diff)
downloadrails-b067bf788454106de761ad53af460be9fde48abd.tar.gz
rails-b067bf788454106de761ad53af460be9fde48abd.tar.bz2
rails-b067bf788454106de761ad53af460be9fde48abd.zip
Added Base#attributes that returns a hash of all the attributes with their names as keys and clones of their objects as values #433 [atyp.de]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@386 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/base.rb12
1 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 8be5c7302a..1770026bb4 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -909,6 +909,18 @@ module ActiveRecord #:nodoc:
assign_multiparameter_attributes(multi_parameter_attributes)
end
+ # Returns a hash of all the attributes with their names as keys and clones of their objects as values.
+ def attributes
+ self.attribute_names.inject({}) do |attributes, name|
+ begin
+ attributes[name] = read_attribute(name).clone
+ rescue TypeError
+ attributes[name] = read_attribute(name)
+ end
+ attributes
+ end
+ end
+
# Returns true if the specified +attribute+ has been set by the user or by a database load and is neither
# nil nor empty? (the latter only applies to objects that responds to empty?, most notably Strings).
def attribute_present?(attribute)