Rails model 中的虚拟属性

kevinhua · 2012年04月11日 · 最后由 Rei 回复于 2012年04月11日 · 3290 次阅读

因为要共用一张数据表,所以几个模型共用了一些属性,所以设置了一些虚拟属性:

class Specification::Base
  ... ...
  field :a_array, :type => Array, :default => []
  ... ...
end


class Specification::Movie < Specification::Base
  ... ...
  def authors
    self.a_array
  end

  def authors=(authors)
    self.a_array = authors
  end
end


spmo = Specification::Movie.new

50000.times do |i|
  spmo.authors = ["Andy Lau#{i}", "Jackie Zhou#{i}"]
  spmo.save
  spmo.authors
end


发现 50000 次写、存、读所耗时间几乎一致,约需 27 秒,是不是可以说明虚拟属性的性能是没有问题的,与真实的属性性能相当。

…… 这就是 accessor 而已阿,怎么会有性能问题

需要 登录 后方可回复, 如果你还没有账号请 注册新账号