Rails JSON API, how to deal with nil object in RABL? Rails JSON API, how to deal with nil object in RABL? json json

Rails JSON API, how to deal with nil object in RABL?


I actually found the answer in the RABL documentation trying to solve another issue.

You can add an :unless block to keep it from blowing up trying to access properties on a nil object:

# questions/show.rablobject @questionattributes :id, :textnode(:answer_id, unless: lambda { |question| question.nil? }) do |question|    answer = question.answers.first    answer != nil ? answer.id : nilend

Section in documentation: https://github.com/nesquena/rabl#attributes