Deekor
Deekor

Reputation: 9499

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?

Upvotes: 10

Views: 4604

Answers (1)

Deekor
Deekor

Reputation: 9499

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.

Upvotes: 20

Related Questions