How can I override the attribute assignment in an active record object?

后端 未结 1 1049
情书的邮戳
情书的邮戳 2021-01-13 08:24

I know you can do this with virtual attributes, but what if the column actually exists?

For example, my model has a raw_topic column. When raw_top

1条回答
  •  时光说笑
    2021-01-13 09:13

    You can do it like this:

    def raw_topic=(value)
      # do something with raw topic
      self[:raw_topic] = value
    end
    

    That way you can ensure you still have the raw topic if you need to act on it.

    0 讨论(0)
提交回复
热议问题