blueprinter icon indicating copy to clipboard operation
blueprinter copied to clipboard

Skip field if nil doesn't include associations which are not empty

Open sadda11asm opened this issue 5 years ago • 2 comments

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.

sadda11asm avatar Nov 22 '20 10:11 sadda11asm

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

adamwells avatar Mar 15 '21 20:03 adamwells

Calling public_send will not work if you have custom fields in blueprint. It will throw "undefined method" error

armandsLa avatar Dec 17 '21 09:12 armandsLa

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.

github-actions[bot] avatar Nov 03 '23 01:11 github-actions[bot]