Due date default to n days from now in migration

hello,how can I set the default value of a column in the migration
called due_on with type datetime to n days from now at the time when a
model is created ?

Your migration will have to include a block of code where you go set
those values yourself for those records where due_on is null (which
would be all of them if this is a new field).

After the migration, that sort of logic belongs in your model code, in
an after create callback.

Thanks Ar Chron, - one question: why after create is chosen over
before create in supplying the date that’s n days from now? Could it
be that this is for validation concerns?

In retrospect, before_create should be fine as it occurs right before
Base.save if I recall correctly.

But I would choose a callback which occurs after the validations to
default a value for a new record, unless some validation depends on that
data. It can get pretty messy with the timings, depending on how your
models are related.

Sorry I was out of internet for a few days. – I see, so this is so
that some sort of predetermined/ non-empty date will always be
supplied in a business logic point of view.

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