aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRodrigo Rosenfeld Rosas <rr.rosas@gmail.com>2012-04-06 11:31:29 -0300
committerRodrigo Rosenfeld Rosas <rr.rosas@gmail.com>2012-04-06 11:32:22 -0300
commita49fe6ec2cb9f22a3b18d7db5ae2ee90325e9d09 (patch)
treeaa7b66b9f2e92f449a5af7704c68498a821cb85d
parent871cc8c429fb1ae88d8a3e8f75d52367f0a8c50a (diff)
downloadrails-a49fe6ec2cb9f22a3b18d7db5ae2ee90325e9d09.tar.gz
rails-a49fe6ec2cb9f22a3b18d7db5ae2ee90325e9d09.tar.bz2
rails-a49fe6ec2cb9f22a3b18d7db5ae2ee90325e9d09.zip
Documents that ActiveRecord instances also support []/[]= methods for reading/writing attributes
Only write_attribute and read_attribute were documented and there was no mention to this alternative.
-rw-r--r--activerecord/lib/active_record/base.rb16
1 files changed, 11 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index d25a821688..d9ea48f7dd 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -114,23 +114,29 @@ module ActiveRecord #:nodoc:
# Student.joins(:schools).where(:schools => { :category => 'public' })
# Student.joins(:schools).where('schools.category' => 'public' )
#
+ # == Dynamic assignment
+ #
+ # Sometimes it can be useful to assign some attributes dynamically. You can use
+ # <tt>read_attribute(attr_name, value)</tt>/<tt>write_attribute(attr_name)</tt>
+ # or through model[attr_name]/model[attr_name]=:
+ #
+ # [:deleted, :disabled].each{|p| model[p] = params[p] == 'true' }
+ #
# == Overwriting default accessors
#
# All column values are automatically available through basic accessors on the Active Record
# object, but sometimes you want to specialize this behavior. This can be done by overwriting
- # the default accessors (using the same name as the attribute) and calling
- # <tt>read_attribute(attr_name)</tt> and <tt>write_attribute(attr_name, value)</tt> to actually
- # change things.
+ # the default accessors (using the same name as the attribute):
#
# class Song < ActiveRecord::Base
# # Uses an integer of seconds to hold the length of the song
#
# def length=(minutes)
- # write_attribute(:length, minutes.to_i * 60)
+ # write_attribute(:length, minutes.to_i * 60) # or: self[:length] = minutes.to_i * 60
# end
#
# def length
- # read_attribute(:length) / 60
+ # read_attribute(:length) / 60 # or: self[:length] / 60
# end
# end
#