class

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)
Source

Instance methods

decimal_places
Source
default

Returns the default value of the field if any.

Source
from_db(value) : BigDecimal | Nil

Converts the raw DB value to the corresponding field value.

Source
from_db_result_set(result_set : ::DB::ResultSet) : BigDecimal | Nil

Extracts the field value from a DB result set and returns the right object corresponding to this value.

Source
max_digits
Source
to_column

Returns a migration column object corresponding to the field at hand.

Source
to_db(value) : ::DB::Any

Converts the field value to the corresponding DB value.

Source
validate(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.

Source