aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-05-30 11:06:16 -0600
committerSean Griffin <sean@thoughtbot.com>2015-05-30 11:13:28 -0600
commit6bd2573869eda8b1e4eaa9df2966f814fd9c5d5c (patch)
tree1b3b5deedafce708e430c07f36f4385da8c9a57a /activerecord
parent50914b918e57359193e873e285bcfe38dff12b81 (diff)
downloadrails-6bd2573869eda8b1e4eaa9df2966f814fd9c5d5c.tar.gz
rails-6bd2573869eda8b1e4eaa9df2966f814fd9c5d5c.tar.bz2
rails-6bd2573869eda8b1e4eaa9df2966f814fd9c5d5c.zip
Add docs and changelog entry for 73aab03 [ci skip]
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/attributes.rb11
2 files changed, 15 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index de47660875..fff1f75567 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -11,6 +11,11 @@
*Ryuta Kamizono*
+* Allow proc defaults to be passed to the attributes API. See documentation
+ for examples.
+
+ *Sean Griffin, Kir Shatrov*
+
* SQLite: `:collation` support for string and text columns.
Example:
diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb
index c89099589e..8b2c4c7170 100644
--- a/activerecord/lib/active_record/attributes.rb
+++ b/activerecord/lib/active_record/attributes.rb
@@ -82,6 +82,14 @@ module ActiveRecord
#
# StoreListing.new.my_string # => "new default"
#
+ # class Product < ActiveRecord::Base
+ # attribute :my_default_proc, :datetime, default: -> { Time.now }
+ # end
+ #
+ # Product.new.my_default_proc # => 2015-05-30 11:04:48 -0600
+ # sleep 1
+ # Product.new.my_default_proc # => 2015-05-30 11:04:49 -0600
+ #
# Attributes do not need to be backed by a database column.
#
# class MyModel < ActiveRecord::Base
@@ -204,7 +212,8 @@ module ActiveRecord
#
# +default+ The default value to use when no value is provided. If this option
# is not passed, the previous default value (if any) will be used.
- # Otherwise, the default will be +nil+.
+ # Otherwise, the default will be +nil+. A proc can also be passed, and
+ # will be called once each time a new value is needed.
#
# +user_provided_default+ Whether the default value should be cast using
# +cast+ or +deserialize+.