1

I'm making a JSON API and want to serialize a recipe record from my database. In the JSON response, I want to include the output of @recipe.is_favorited_by?(current_user) along with some of the recipe's attributes.

I saw from the docs that I could call: render json: @recipe, methods: :is_favorited_by? - but I'm not sure how to pass in current_user as an argument.

What call would I need to make in order to include the results of @recipe.is_favorited_by?(current_user) in the response?

1 Answers1

0

You can add current_user attribute to your model & set it before your json execute.

See stackoverflow question link Ruby to_json :methods arguments

Community
  • 1
  • 1
Amol Udage
  • 2,917
  • 19
  • 27
  • Thanks for the help, but I'm wondering if there's a more extensible way of doing this. If there are other methods that require other arguments, I would have to do this for each one if I use this approach. – Hank-Roughknuckles May 18 '16 at 12:11