Search code examples
ruby-on-rails-3redmine-pluginsrails-console

how to replace null by value of an attribute in rails console


I'm writing a redmine plugin where model is Allissue. I added two attributes project_name and create_date. First, I added column project_name and assigned them some values. Second, I added one more column created_date for which values become null as shown in figure. enter image description here

I want to replace null with integer values. It is very basic question but I don't know as I'm a newbie. Can you suggest me the syntax to add values. Thanks


Solution

  • For this to happen automatically, add something like this to your model:

    class Allissue
      ...
      before_save do |a|
        a.create_date = Time.now # or whatever you want it to be
      end
      ...
    end
    

    or manually, you would do (for all multiple objects):

    Allissue.all.each{|a| a.create_date = Time.now; a.save}
    

    or (for a single object):

    a = Allissue.find(1)
    a.create_date = Time.now
    a.save
    

    I hope this helps.