Marten::DB::Field::Decimal
Inherits Marten::DB::Field::Base < Reference < Object
A fixed-precision decimal model field.
Decimal fields are persisted as numeric(max_digits, decimal_places) columns and are exposed as
BigDecimal objects in Crystal. Both max_digits and decimal_places are required:
class Product < Marten::Model
field :id, :big_int, primary_key: true, auto: true
field :price, :decimal, max_digits: 10, decimal_places: 2
end
product = Product.new(price: 19.99)
product.price = 20
Constructors
new(id : ::String, max_digits : Int32, decimal_places : Int32, primary_key = false, default : BigDecimal | Nil = nil, blank = false, null = false, unique = false, index = false, db_column = nil)
SourceInstance methods
decimal_places
SourceExtracts the field value from a DB result set and returns the right object corresponding to this value.
max_digits
Sourcevalidate(record, value)
Runs custom validation logic for a specific model field and model object.
This method should be overridden for each field implementation that requires custom validation logic.