EnumerizeカラムをJSON化したときに数値で出力したい
掲題の通り
目的
controllerで@model.to_json
とかrender :json, @model
とか雑にJSONを返しており
enumerizeを使い始めたことにより数値だったカラムが文字列で返るようになってしまったので
controllerやviewを変えることなく数値で返るようにしたい
方法
# 対象モデル class Model def read_attribute_for_serialization(key) if [:column1, :column2].include?(key.to_sym) __send__(key).value else __send__(key) end end end
としてやればto_json
なりas_json
なりしたときに数値出力することが出来る。