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


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
    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
    => self.created_by= <== Not set (in database it’s not set either)


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

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

  • “self.created_by = Suberb.current_user_gregs” as the syntax worked
  • doh! My database column was a datetime not a string (prior to fixing
    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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs