Вы можете переопределить as_json
:
# clean_to_json.rb
module CleanToJson
def as_json(options = nil)
super(options).tap do |json|
json.delete_if{|k,v| v.nil?}.as_json unless options.try(:delete, :null)
end
end
end
# foo.rb
class Foo < ActiveRecord::Base
include CleanToJson
end
Использование:
@foo.as_json # Only present attributes
@foo.as_json(:null => true) # All attributes (former behavior)