ActiveRecord attribute override

I have a model with a complete: logical column that I would like to override, so I can add conditional code.

I had never redefined the ActiveRecord attribute before and wanted to know if the method below is good practice?

class Article < ActiveRecord::Base
  def completed=(b)
    write_attribute(:completed, b)
    # IF b is true then do something
  end
end
+3
source share
1 answer

Your approach is wonderful. The proposed method is the one described in the ActiveRecord Documentation (scroll down to the default Overwriting Devices heading )

, , , , before_save .

+3

Source: https://habr.com/ru/post/1745852/


All Articles