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

Reply

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

Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.