All threads / Not able to override a column with default value

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Not able to override a column with default value

Jose asked in Rails

I have created this in migration,

created_by VARCHAR (64) DEFAULT CURRENT_USER,
updated_by VARCHAR (64) DEFAULT CURRENT_USER

By default the current user values are stored but when i try to override like,

user = User.new
user.created_by = "New User"
user.updated_by = "New User"
user.save

The above code is not saving the "New User" value into created_by and updated_by column and still its storing the adefault value on save.

I tried to override by adding the below in model

def created_by=(created_by_user)
    write_attribute(:created_by, created_by_user)
  end

  def updated_by=(updated_by_user)
    write_attribute(:updated_by, updated_by_user)
  end

also using super

def created_by=(created_by_user)
    super created_by_user
  end

  def updated_by=(updated_by_user)
    super updated_by_user
  end

Its not workig still and i also checkt at the db level there are no triggers.

Thanks in advance

This works fine with update_colum and not update_attributes or save! and from code there are no callbacks triggered for this model.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,344+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.