我从数据库中得到一条记录,如下所示:
@foo = Foo.find(params[:foo_id])
我需要整个 @foo
记录,但我需要使用 ActiveRecord 中的 value
字段(BigDecimal 类型)。我想我可以做到这一点(?):
@val = @foo.value
但是如何将@val
转换为字符串?我找到了一个 to_str
方法,但我得到:
undefined method `to_str' for #<BigDecimal:7f88d4e5fa10,'0.5155170589 999999E2',27(45)>
当我尝试在 @val
上调用 to_str
时,如下所示:
@val = @foo.value.to_str # ERROR here
最佳答案
您需要使用 to_s
方法,这是在 Ruby 中将任何对象转换为字符串的标准方法。
关于ruby - 将 BigDecimal ActiveRecord 转换为 Ruby 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17632344/