Sometimes we need to run some logic before updating attribute.

To do so – we can overwrite object.name= method like this:

class Cosik < ActiveRecord::Base
  def name=(str)
    # Doing some stuff
    param.downcase.gsub(/[^a-zA-Z 0-9]/, ""))!.gsub(/\s/,'-')! if param
    write_attribute(:name, str)
  end
end