Skip to content

Instantly share code, notes, and snippets.

@novohispano
Last active September 15, 2015 14:14
Show Gist options
  • Save novohispano/14960bb7638066820498 to your computer and use it in GitHub Desktop.
Save novohispano/14960bb7638066820498 to your computer and use it in GitHub Desktop.
class ItemSerializer < ActiveModel::Serializer
attributes :id,
:name,
:description,
:image_url,
:created_at,
:updated_at,
:order_count
has_many :order_items
has_many :orders, through: :order_items
def order_count
object.orders.count
end
end
json.extract! @item, :id,
:name,
:description,
:created_at,
:updated_at
json.order_count @item.orders.count
json.orders @item.orders, :amount,
:user_id,
:created_at,
:updated_at
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment