我有一个字段loan_rate_legs
,它是一个JSON类型
但是,我想将upperbound_rate,lowerbound_rate参数从STRING类型转换为Float类型
[
...
"loan_rate_legs"=>
[{"duration"=>"6M", "upperbound_rate"=>"1.2", "lowerbound_rate"=>"17.2"},
{"duration"=>"16M", "upperbound_rate"=>"31.2", "lowerbound_rate"=>"17.2"}
]}
]
我使用以下强力params方法进行了手动转换,但它看起来很难看。
任何好的替代解决方案?
def loan_plan_params
cleaned_params = params.require(:loan_plan).permit(
:id,
:created_at,
:updated_at,
:loan_rate_legs => [:duration, :upperbound_rate, :lowerbound_rate]
)
cleaned_params['loan_rate_legs'] = cleaned_params['loan_rate_legs'].collect do |loan_rate_leg|
{
duration: loan_rate_leg["duration"],
upperbound_rate: loan_rate_leg["upperbound_rate"].to_f,
lowerbound_rate: loan_rate_leg["lowerbound_rate"].to_f,
}
end
cleaned_params
end