How to override a column in Rails model?

前端 未结 4 429
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 09:31

I have a model for one of my database tables. I want to override the column name for that particular table. How would I achieve it.

For example, let my table be call

4条回答
  •  一向
    一向 (楼主)
    2020-12-14 10:09

    You can override the col_a method. Use the read_attribute method to read the value in database. Something like this:

    def col_a
      if self.read_attribute(:col_a).to_s.end_with?('0')
        0
      else
        self.read_attribute(:col_a)
      end
    end
    

提交回复
热议问题