看到代码可以 self[:xxx]
这样使用,请问这样和 self.xxx
有什么区别呢?
找不到 self 下标的资料
#2 楼 @kingwkb ActiveRecord 的 base.rb 文件
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
# (Alias for the protected read_attribute method).
def [](attr_name)
read_attribute(attr_name)
end
# Updates the attribute identified by <tt>attr_name</tt> with the specified +value+.
# (Alias for the protected write_attribute method).
def []=(attr_name, value)
write_attribute(attr_name, value)
end
#5 楼 @kingwkb
前面贴给你的是 2.3 的路径
ActiveRecord 3.2 的路径是
lib/active_record/attribute_methods.rb
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
# (Alias for the protected read_attribute method).
def [](attr_name)
read_attribute(attr_name)
end
# Updates the attribute identified by <tt>attr_name</tt> with the specified +value+.
# (Alias for the protected write_attribute method).
def []=(attr_name, value)
write_attribute(attr_name, value)
end