blueprinter
blueprinter copied to clipboard
Skip field if nil doesn't include associations which are not empty
Blueprinter.configure do |config|
config.if = ->(field_name, obj, _options) { obj[field_name].present? }
end
I am using this to not include nil fields to the response, but it also takes all associations out too.
Is this with activerecord models? It's likely because the regular attributes are accessible with [] but associations are not - maybe try
Blueprinter.configure do |config|
config.if = ->(field_name, obj, _options) { obj.public_send(field_name).present? }
end
Calling public_send will not work if you have custom fields in blueprint. It will throw "undefined method" error
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.