The attribute API lets me set default values like this
class Enrollment < ActiveRecord::Base
attribute :end_time, :datetime, default: -> { Time.now }
end
Is it possible to set the default value based on a column in the model? The following does not work
class CreateEnrollments < ActiveRecord::Migration[6.0]
def change
create_table :enrollments do |t|
t.datetime :starts_at
end
end
end
class Enrollment < ActiveRecord::Base
attribute :end_time, :datetime, default: -> { starts_at.nil? ? Time.now : starts_at + 1.hour }
end