read_attribute 根据属性名,获取其值。
read_attribute
和 self[:x] 等价
read_attribute(:name) # 等价于 self[:name] # 等价于 self.name
数据从 attributes 里获取,不同于直接 self.name 它获取的是真实数据。
attributes
self.name
如果字段用于存储图片信息,并且我们有默认图片,则没有图片时: self.image 返回的是默认图片信息, 而 self[:image] 返回 nil 这才是真实信息, 这和 self.attributes 里的 image 信息一致。
self.image
self[:image]
nil
self.attributes
image
那为什么不用 attributes[:name] 而用 read_attribute[:name] ? 因为性能,前者要把所有的属性都找出来,然后取 name 属性; 而后者可以直接获取 name 属性。
attributes[:name]
read_attribute[:name]
name
属性名,加后缀 = 进行赋值。
=
区别于 attr_writer, 这里的写和数据库操作有关。
最后更新于5年前