Skip to content

Instantly share code, notes, and snippets.

@csexton
Created March 14, 2014 04:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csexton/9542200 to your computer and use it in GitHub Desktop.
Save csexton/9542200 to your computer and use it in GitHub Desktop.
Extend the jbuilder api to support JSON API conventions
class Jbuilder
def links! model, *args
args.each do |arg|
if model.reflections[arg].collection?
_set_value arg, model.send(arg).map(&:id)
else
_set_value arg, model.send(arg).id
end
end
end
end
{
"posts": [
{
"links": {
"comments": [1,2,3,4,5],
"user": 2,
"tags": [30,31,32,33]
}
}
]
}
json.posts @posts do |post|
json.links do
json.links! post, :comments, :user, :tags
end
end
@guillec
Copy link

guillec commented Mar 14, 2014

Yes please!

@simplyb
Copy link

simplyb commented Mar 14, 2014

πŸ‘ πŸ”₯ πŸ‘ πŸ”₯ πŸ‘ πŸ”₯ πŸ‘ πŸ”₯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment