Skip to content

Instantly share code, notes, and snippets.

@metaskills
Created May 18, 2011 01:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save metaskills/977846 to your computer and use it in GitHub Desktop.
Save metaskills/977846 to your computer and use it in GitHub Desktop.
If your not returning ruby primitives and other objects that know how to respond to #as_json from #as_jons - Your doing it wrong.
class User < ActiveRecord::Base
has_many :columns
def as_json(options={})
attributes.slice(:id, :email, :uuid).merge(:columns => columns)
end
end
class Column < ActiveRecord::Base
belongs_to :user
has_many :boxes
def as_json(options=nil)
attributes.slice(*JSON_ATTRS).merge(:boxes => boxes)
end
end
class Box < ActiveRecord::Base
belongs_to :column
has_many :bookmarks
def as_json(options=nil)
attributes.slice(:id, :column_id, :title, :style, :collapsed, :position).merge(:bookmarks => bookmarks)
end
end
class Bookmark < ActiveRecord::Base
belongs_to :box
def as_json(options=nil)
attributes.slice(:id, :owner_id, :owner_type, :url, :name, :position)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment