我需要将string字段转换为integer并使用enum.在不丢失数据的情况下,最好的方法是什么?
这是当前的迁移:
class CreateSystems < ActiveRecord::Migration
def change
create_table :systems do |t|
t.string :operation
t.string :status
t.timestamps null: false
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后我改变字段的类型,如下所示:
class ChangeColumnsForSystems < ActiveRecord::Migration
def change
change_column :systems, :operation, :integer
change_column :systems, :status, :integer
end
end
Run Code Online (Sandbox Code Playgroud)
并更新模型文件.
/app/models/system.rb
...
enum operation { start: 0, stop: 1 }
enum status { init: 0, working: 1, complete: 2 }
...
Run Code Online (Sandbox Code Playgroud)
如何更新旧数据?
找到n个骰子滚动概率概率的最佳解决方案是什么?我正在通过寻找解决它
xx这就是我到目前为止所做的.
# sides - number of sides on one die
def get_mean(sides)
(1..sides).inject(:+) / sides.to_f
end
def get_variance(sides)
mean_of_squares = ((1..sides).inject {|sum, side| sum + side ** 2}) / sides.to_f
square_mean = get_mean(sides) ** 2
mean_of_squares - square_mean
end
def get_sigma(variance)
variance ** 0.5
end
# x - the number of points in question
def get_z_score(x, mean, sigma)
(x - mean) / sigma.to_f
end
# Converts z_score to probability
def z_to_probability(z) …Run Code Online (Sandbox Code Playgroud) 如何使用ruby将z分数转换为概率?
例:
z_score = 0
probability should be 0.5
z_score = 1.76
probability should be 0.039204
Run Code Online (Sandbox Code Playgroud)