Roy Roy - 6 months ago 16
Ruby Question

How to generate model with restriction on data precision, scale in ruby on rails

I am trying to generate the following a model named

PriceUsdOneMinIntervals
, but unable to apply the format for precision and scale correctly, can someone please advise how to correct the syntax in the following:

rails generate model PriceUsdOneMinInterval unix_timestamp:integer usd_high:decimal :precision[20] :scale[8] usd_low:decimal :precision[20] :scale[8] usd_open:decimal :precision[20] :scale[8] usd_close:decimal :precision[20] scale[8]


I have referred to this api while making the above statement.

Answer

TRy this

rails generate model PriceUsdOneMinInterval :unix_timestamp, :integer, 
:usd_high, :decimal, precision: 20, scale: 8, :usd_low, :decimal, precision: 20, 
scale: 8, :usd_open, :decimal, precision: 20, scale: 8, 
:usd_close, :decimal, precision: 20, scale: 8

OR

generate migration with just field names and data_types and then change you can change the values in migration

def change
    add_column :price_usd_one_min_intervals, :unix_timestamp, :integer
    add_column :price_usd_one_min_intervals, :usd_high, :decimal, precision: 20, :scale: 8
    add_column :price_usd_one_min_intervals, :usd_low, :decimal, precision: 20, scale: 8
    add_column :price_usd_one_min_intervals, :usd_open, :decimal, precision: 20, scale: 8
    add_column :price_usd_one_min_intervals, :usd_close, :decimal, precision: 20, scale: 8
end 
Comments