ActiveRecord对象属性的自定义类型转换方法

时间:2010-12-17 21:06:06

标签: ruby-on-rails activerecord

是否可以覆盖ActiveRecord对象的属性的类型转换。例如

class Project < ActiveRecord::Base

  def name.to_s
    #some logic to act on self.name
  end
end

我知道我可以用帮助器做到这一点,但只是好奇这是否可能。

1 个答案:

答案 0 :(得分:1)

如果您只想操纵名称返回...请执行此操作...

class Project < ActiveRecord::Base
  def name
    this_name = read_attribute(:name)

    # do some stuff with this_name

    this_name #return this_name
  end
end
相关问题