Why doesn't this work (setting attribute in "before_create")

Hi,

Can anyone spot why the below doesn’t work?

  • I have a “created_by” column in my create table in the dB
  • I have a “before_create” line, which definitely gets called
  • I tried a few different ways to set the “created_by” attribute in the
    model but it doesn’t seem to work, i.e. doesn’t appear in the database
    =============================
    class Contact < ActiveRecord::Base
    before_create :generic_fields
    private
    def generic_fields
    created_by=“test text” <=== DOESN’T SET IT
    self.created_by=“test text” <=== DOESN’T SET IT
    @created_by=“test text” <=== DOESN’T SET IT
    end
    end
    =============================
    => self.created_by= <== Not set (in database it’s not set either)

Tks

bump (anyone able to point out why this doesn’t work below please -
thanks)

solved it guys (just in case someone started to look at this)

  • “self.created_by = Suberb.current_user_gregs” as the syntax worked
    but
    noting
  • doh! My database column was a datetime not a string (prior to fixing
    this
    the column was showing NULL)

I wonder whether rails should have really picked up on this error? The
only way I was able to discover this was via code/db review.