Difference between self.field and self[:field] in model callbacks?

I have a before_save callback that does a bit of housekeeping to the
model data before it’s saved.

The Rails documentation often attests the use of self.field to change
data before it’s saved, eg:

def before_save
self.payment_due ||= Time.now + 30.days

But in my case doing self.field=‘something’ in the before_save doesn’t
appear to make any difference. However it works with self[:field]
which is the same as write_attribute(:field,‘something’)

Can someone explain what the difference is? (And ideally how
self.@field fits into all this, too)

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