Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails render json add custom attribute

I have a method call:

def group
    render :json => Group.find(params[:id])
end

Which renders:

{
  id: 1,
  name: "Name Here",
  description: "Description Here",
  created_at: "2014-11-24T19:10:53.609Z",
  updated_at: "2014-11-24T19:10:53.609Z"
}

I would like to also append a custom attribute the the group model being rendered. For example I'd like the group JSON to include an attribute message : "Hello World"

How can I do this?

like image 449
Deekor Avatar asked Dec 08 '14 19:12

Deekor


1 Answers

Adding a method to my model called message that returns a string and then calling,

render :json => Group.find(params[:id]), :methods => :message 

did the trick.

like image 79
Deekor Avatar answered Oct 21 '22 13:10

Deekor