Hi, I’m using ActiveModel::Attributes and ActiveModel::Type for json data from some API systems. And got into trouble by casting of ActiveModel::Type::String
here is the rails code.
private def cast_value(value) case value when ::String then ::String.new(value) when true then "t" when false then "f" else value.to_s end end
and here is my sample code
class Test include ActiveModel::Model include ActiveModel::Attributes attribute :flag, :string end test = Test.new(flag: false) test.flag => "f"
My expectation is that when I set boolean
false to the string attribute
:flag, it will return
"false" as string value. This is because simple
to_s method does like below.
false.to_s => "false"
what’s the reason or any background to cast
"f" instead of
I would like to propose to change this casting to be same as
to_s and returns