Rails - Add serializer for group by resources
up vote
0
down vote
favorite
Below is my query which is returning category and its co-responding products.
products = Product
.fetch_products(current_resource_owner)
.where("product_warehouses.on_sale = true")
.group_by(&:category).map { |c, p| [c, p.take(4)] }
Below is the sample response for above query
[
[
{
"id": 1,
"name": "Beer",
"created_at": "2018-05-28T01:30:23.593-07:00",
"updated_at": "2018-08-10T02:15:19.155-07:00",
"image_file_name": "mirassou-pinot-noir-abv-135-750-ml.jpg",
"image_content_type": "image/jpeg",
"image_file_size": 30794,
"image_updated_at": "2018-08-10T02:15:18.521-07:00",
"lightspeed_category_id": null
},
[
{
"id": 3029,
"name": "4 Sons Surf City Pale Ale ABV: 5.9% 12 fl oz 6-Pack",
"description": "4 Sons Surf City Pale Ale ABV: 5.9% 12 fl oz 6-Pack",
"abv": null,
"sub_category_id": 58,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:17:59.410-07:00",
"updated_at": "2018-08-02T22:38:15.998-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 1,
"manufacture_date": null,
"system_sku": "COD0003029",
"sold": 1,
"lightspeed_item_id": 4121,
"quantity": null,
"warehouse_id": 2,
"price": "11.99",
"on_sale": true,
"sale_price": "5.99",
"discount": "50.04",
"is_wishlist": false,
"is_in_cart": true,
"cart_quantity": 1
}
]
],
[
{
"id": 2,
"name": "Spirits",
"created_at": "2018-05-28T01:34:22.975-07:00",
"updated_at": "2018-05-28T01:34:22.975-07:00",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"lightspeed_category_id": null
},
[
{
"id": 3032,
"name": "Nucano Joven Espadín Mezcal ABV: 45% 750 mL",
"description": "Nucano Joven Espadín Mezcal ABV: 45% 750 mL",
"abv": null,
"sub_category_id": 76,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:26:38.227-07:00",
"updated_at": "2018-08-10T02:10:22.804-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "myerss-original-dark-rum-proof-80-375-ml.jpg",
"image_content_type": "image/jpeg",
"image_file_size": 65161,
"image_updated_at": "2018-08-10T02:10:22.250-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003032",
"sold": 0,
"lightspeed_item_id": 4151,
"quantity": "50_ml",
"warehouse_id": 2,
"price": "61.99",
"on_sale": true,
"sale_price": "40.99",
"discount": "33.88",
"is_wishlist": false,
"is_in_cart": true,
"cart_quantity": 1
},
{
"id": 3010,
"name": "Espolón Tequila Reposado ABV: 40% 750 mL",
"description": "Espolón Tequila Reposado ABV: 40% 750 mL",
"abv": null,
"sub_category_id": 32,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-18T23:28:05.801-07:00",
"updated_at": "2018-07-18T23:28:06.078-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003010",
"sold": 0,
"lightspeed_item_id": 4082,
"quantity": "375_ml",
"warehouse_id": 2,
"price": "23.99",
"on_sale": true,
"sale_price": "10.11",
"discount": "57.86",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3023,
"name": "Azunia Silver Tequila 50 mL",
"description": "Azunia Silver Tequila 50 mL",
"abv": null,
"sub_category_id": 33,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-20T05:43:32.984-07:00",
"updated_at": "2018-07-23T01:22:05.127-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "socfo9k2cvjovliqbsyj",
"image_content_type": "image/jpeg",
"image_file_size": 22150,
"image_updated_at": "2018-07-20T05:43:32.614-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003023",
"sold": 0,
"lightspeed_item_id": 1811,
"quantity": "180_ml",
"warehouse_id": 2,
"price": "33.66",
"on_sale": true,
"sale_price": "23.66",
"discount": "29.71",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3020,
"name": "Hornitos Silver Tequila Proof: 80 375 mL",
"description": "Hornitos Silver Tequila Proof: 80 375 mL",
"abv": null,
"sub_category_id": 33,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-20T05:43:15.107-07:00",
"updated_at": "2018-07-20T05:43:15.972-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "dejbqd9vkzbqiywmj8ye",
"image_content_type": "image/jpeg",
"image_file_size": 57468,
"image_updated_at": "2018-07-20T05:43:13.290-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003020",
"sold": 0,
"lightspeed_item_id": 1806,
"quantity": "750_ml",
"warehouse_id": 2,
"price": "15.99",
"on_sale": true,
"sale_price": "12.99",
"discount": "18.76",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
}
]
],
[
{
"id": 3,
"name": "Wine",
"created_at": "2018-05-28T01:47:47.497-07:00",
"updated_at": "2018-05-28T01:47:47.497-07:00",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"lightspeed_category_id": null
},
[
{
"id": 3031,
"name": "Vanderpump 2017 Rosé ABV: 13% 750 mL",
"description": "Vanderpump 2017 Rosé ABV: 13% 750 mL",
"abv": null,
"sub_category_id": 98,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:18:02.332-07:00",
"updated_at": "2018-07-31T00:18:02.483-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 3,
"manufacture_date": null,
"system_sku": "COD0003031",
"sold": 0,
"lightspeed_item_id": 4123,
"quantity": null,
"warehouse_id": 2,
"price": "23.99",
"on_sale": true,
"sale_price": "12.99",
"discount": "45.85",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3027,
"name": "Vina Robles Monterey 2017 Chardonnay ABV: 13.5% 187 mL",
"description": "Vina Robles Monterey 2017 Chardonnay ABV: 13.5% 187 mL",
"abv": null,
"sub_category_id": 18,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:10:03.077-07:00",
"updated_at": "2018-07-31T00:10:03.223-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 3,
"manufacture_date": null,
"system_sku": "COD0003027",
"sold": 0,
"lightspeed_item_id": 4157,
"quantity": null,
"warehouse_id": 2,
"price": "5.99",
"on_sale": true,
"sale_price": "3.99",
"discount": null,
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
}
]
]
]
I want to serialize this resouces like below
Success.new({
categories: array_serializer.new(categories, each_serializer: Api::V1::CategoriesSerializer),
all_products: array_serializer.new(@products, each_serializer: Api::V1::Products::GetAllProductsSerializer,
}, '', 200)
Is there any way to serialize group by resources ?
ruby-on-rails ruby active-model-serializers
add a comment |
up vote
0
down vote
favorite
Below is my query which is returning category and its co-responding products.
products = Product
.fetch_products(current_resource_owner)
.where("product_warehouses.on_sale = true")
.group_by(&:category).map { |c, p| [c, p.take(4)] }
Below is the sample response for above query
[
[
{
"id": 1,
"name": "Beer",
"created_at": "2018-05-28T01:30:23.593-07:00",
"updated_at": "2018-08-10T02:15:19.155-07:00",
"image_file_name": "mirassou-pinot-noir-abv-135-750-ml.jpg",
"image_content_type": "image/jpeg",
"image_file_size": 30794,
"image_updated_at": "2018-08-10T02:15:18.521-07:00",
"lightspeed_category_id": null
},
[
{
"id": 3029,
"name": "4 Sons Surf City Pale Ale ABV: 5.9% 12 fl oz 6-Pack",
"description": "4 Sons Surf City Pale Ale ABV: 5.9% 12 fl oz 6-Pack",
"abv": null,
"sub_category_id": 58,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:17:59.410-07:00",
"updated_at": "2018-08-02T22:38:15.998-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 1,
"manufacture_date": null,
"system_sku": "COD0003029",
"sold": 1,
"lightspeed_item_id": 4121,
"quantity": null,
"warehouse_id": 2,
"price": "11.99",
"on_sale": true,
"sale_price": "5.99",
"discount": "50.04",
"is_wishlist": false,
"is_in_cart": true,
"cart_quantity": 1
}
]
],
[
{
"id": 2,
"name": "Spirits",
"created_at": "2018-05-28T01:34:22.975-07:00",
"updated_at": "2018-05-28T01:34:22.975-07:00",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"lightspeed_category_id": null
},
[
{
"id": 3032,
"name": "Nucano Joven Espadín Mezcal ABV: 45% 750 mL",
"description": "Nucano Joven Espadín Mezcal ABV: 45% 750 mL",
"abv": null,
"sub_category_id": 76,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:26:38.227-07:00",
"updated_at": "2018-08-10T02:10:22.804-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "myerss-original-dark-rum-proof-80-375-ml.jpg",
"image_content_type": "image/jpeg",
"image_file_size": 65161,
"image_updated_at": "2018-08-10T02:10:22.250-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003032",
"sold": 0,
"lightspeed_item_id": 4151,
"quantity": "50_ml",
"warehouse_id": 2,
"price": "61.99",
"on_sale": true,
"sale_price": "40.99",
"discount": "33.88",
"is_wishlist": false,
"is_in_cart": true,
"cart_quantity": 1
},
{
"id": 3010,
"name": "Espolón Tequila Reposado ABV: 40% 750 mL",
"description": "Espolón Tequila Reposado ABV: 40% 750 mL",
"abv": null,
"sub_category_id": 32,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-18T23:28:05.801-07:00",
"updated_at": "2018-07-18T23:28:06.078-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003010",
"sold": 0,
"lightspeed_item_id": 4082,
"quantity": "375_ml",
"warehouse_id": 2,
"price": "23.99",
"on_sale": true,
"sale_price": "10.11",
"discount": "57.86",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3023,
"name": "Azunia Silver Tequila 50 mL",
"description": "Azunia Silver Tequila 50 mL",
"abv": null,
"sub_category_id": 33,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-20T05:43:32.984-07:00",
"updated_at": "2018-07-23T01:22:05.127-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "socfo9k2cvjovliqbsyj",
"image_content_type": "image/jpeg",
"image_file_size": 22150,
"image_updated_at": "2018-07-20T05:43:32.614-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003023",
"sold": 0,
"lightspeed_item_id": 1811,
"quantity": "180_ml",
"warehouse_id": 2,
"price": "33.66",
"on_sale": true,
"sale_price": "23.66",
"discount": "29.71",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3020,
"name": "Hornitos Silver Tequila Proof: 80 375 mL",
"description": "Hornitos Silver Tequila Proof: 80 375 mL",
"abv": null,
"sub_category_id": 33,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-20T05:43:15.107-07:00",
"updated_at": "2018-07-20T05:43:15.972-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "dejbqd9vkzbqiywmj8ye",
"image_content_type": "image/jpeg",
"image_file_size": 57468,
"image_updated_at": "2018-07-20T05:43:13.290-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003020",
"sold": 0,
"lightspeed_item_id": 1806,
"quantity": "750_ml",
"warehouse_id": 2,
"price": "15.99",
"on_sale": true,
"sale_price": "12.99",
"discount": "18.76",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
}
]
],
[
{
"id": 3,
"name": "Wine",
"created_at": "2018-05-28T01:47:47.497-07:00",
"updated_at": "2018-05-28T01:47:47.497-07:00",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"lightspeed_category_id": null
},
[
{
"id": 3031,
"name": "Vanderpump 2017 Rosé ABV: 13% 750 mL",
"description": "Vanderpump 2017 Rosé ABV: 13% 750 mL",
"abv": null,
"sub_category_id": 98,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:18:02.332-07:00",
"updated_at": "2018-07-31T00:18:02.483-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 3,
"manufacture_date": null,
"system_sku": "COD0003031",
"sold": 0,
"lightspeed_item_id": 4123,
"quantity": null,
"warehouse_id": 2,
"price": "23.99",
"on_sale": true,
"sale_price": "12.99",
"discount": "45.85",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3027,
"name": "Vina Robles Monterey 2017 Chardonnay ABV: 13.5% 187 mL",
"description": "Vina Robles Monterey 2017 Chardonnay ABV: 13.5% 187 mL",
"abv": null,
"sub_category_id": 18,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:10:03.077-07:00",
"updated_at": "2018-07-31T00:10:03.223-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 3,
"manufacture_date": null,
"system_sku": "COD0003027",
"sold": 0,
"lightspeed_item_id": 4157,
"quantity": null,
"warehouse_id": 2,
"price": "5.99",
"on_sale": true,
"sale_price": "3.99",
"discount": null,
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
}
]
]
]
I want to serialize this resouces like below
Success.new({
categories: array_serializer.new(categories, each_serializer: Api::V1::CategoriesSerializer),
all_products: array_serializer.new(@products, each_serializer: Api::V1::Products::GetAllProductsSerializer,
}, '', 200)
Is there any way to serialize group by resources ?
ruby-on-rails ruby active-model-serializers
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Below is my query which is returning category and its co-responding products.
products = Product
.fetch_products(current_resource_owner)
.where("product_warehouses.on_sale = true")
.group_by(&:category).map { |c, p| [c, p.take(4)] }
Below is the sample response for above query
[
[
{
"id": 1,
"name": "Beer",
"created_at": "2018-05-28T01:30:23.593-07:00",
"updated_at": "2018-08-10T02:15:19.155-07:00",
"image_file_name": "mirassou-pinot-noir-abv-135-750-ml.jpg",
"image_content_type": "image/jpeg",
"image_file_size": 30794,
"image_updated_at": "2018-08-10T02:15:18.521-07:00",
"lightspeed_category_id": null
},
[
{
"id": 3029,
"name": "4 Sons Surf City Pale Ale ABV: 5.9% 12 fl oz 6-Pack",
"description": "4 Sons Surf City Pale Ale ABV: 5.9% 12 fl oz 6-Pack",
"abv": null,
"sub_category_id": 58,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:17:59.410-07:00",
"updated_at": "2018-08-02T22:38:15.998-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 1,
"manufacture_date": null,
"system_sku": "COD0003029",
"sold": 1,
"lightspeed_item_id": 4121,
"quantity": null,
"warehouse_id": 2,
"price": "11.99",
"on_sale": true,
"sale_price": "5.99",
"discount": "50.04",
"is_wishlist": false,
"is_in_cart": true,
"cart_quantity": 1
}
]
],
[
{
"id": 2,
"name": "Spirits",
"created_at": "2018-05-28T01:34:22.975-07:00",
"updated_at": "2018-05-28T01:34:22.975-07:00",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"lightspeed_category_id": null
},
[
{
"id": 3032,
"name": "Nucano Joven Espadín Mezcal ABV: 45% 750 mL",
"description": "Nucano Joven Espadín Mezcal ABV: 45% 750 mL",
"abv": null,
"sub_category_id": 76,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:26:38.227-07:00",
"updated_at": "2018-08-10T02:10:22.804-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "myerss-original-dark-rum-proof-80-375-ml.jpg",
"image_content_type": "image/jpeg",
"image_file_size": 65161,
"image_updated_at": "2018-08-10T02:10:22.250-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003032",
"sold": 0,
"lightspeed_item_id": 4151,
"quantity": "50_ml",
"warehouse_id": 2,
"price": "61.99",
"on_sale": true,
"sale_price": "40.99",
"discount": "33.88",
"is_wishlist": false,
"is_in_cart": true,
"cart_quantity": 1
},
{
"id": 3010,
"name": "Espolón Tequila Reposado ABV: 40% 750 mL",
"description": "Espolón Tequila Reposado ABV: 40% 750 mL",
"abv": null,
"sub_category_id": 32,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-18T23:28:05.801-07:00",
"updated_at": "2018-07-18T23:28:06.078-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003010",
"sold": 0,
"lightspeed_item_id": 4082,
"quantity": "375_ml",
"warehouse_id": 2,
"price": "23.99",
"on_sale": true,
"sale_price": "10.11",
"discount": "57.86",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3023,
"name": "Azunia Silver Tequila 50 mL",
"description": "Azunia Silver Tequila 50 mL",
"abv": null,
"sub_category_id": 33,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-20T05:43:32.984-07:00",
"updated_at": "2018-07-23T01:22:05.127-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "socfo9k2cvjovliqbsyj",
"image_content_type": "image/jpeg",
"image_file_size": 22150,
"image_updated_at": "2018-07-20T05:43:32.614-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003023",
"sold": 0,
"lightspeed_item_id": 1811,
"quantity": "180_ml",
"warehouse_id": 2,
"price": "33.66",
"on_sale": true,
"sale_price": "23.66",
"discount": "29.71",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3020,
"name": "Hornitos Silver Tequila Proof: 80 375 mL",
"description": "Hornitos Silver Tequila Proof: 80 375 mL",
"abv": null,
"sub_category_id": 33,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-20T05:43:15.107-07:00",
"updated_at": "2018-07-20T05:43:15.972-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "dejbqd9vkzbqiywmj8ye",
"image_content_type": "image/jpeg",
"image_file_size": 57468,
"image_updated_at": "2018-07-20T05:43:13.290-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003020",
"sold": 0,
"lightspeed_item_id": 1806,
"quantity": "750_ml",
"warehouse_id": 2,
"price": "15.99",
"on_sale": true,
"sale_price": "12.99",
"discount": "18.76",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
}
]
],
[
{
"id": 3,
"name": "Wine",
"created_at": "2018-05-28T01:47:47.497-07:00",
"updated_at": "2018-05-28T01:47:47.497-07:00",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"lightspeed_category_id": null
},
[
{
"id": 3031,
"name": "Vanderpump 2017 Rosé ABV: 13% 750 mL",
"description": "Vanderpump 2017 Rosé ABV: 13% 750 mL",
"abv": null,
"sub_category_id": 98,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:18:02.332-07:00",
"updated_at": "2018-07-31T00:18:02.483-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 3,
"manufacture_date": null,
"system_sku": "COD0003031",
"sold": 0,
"lightspeed_item_id": 4123,
"quantity": null,
"warehouse_id": 2,
"price": "23.99",
"on_sale": true,
"sale_price": "12.99",
"discount": "45.85",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3027,
"name": "Vina Robles Monterey 2017 Chardonnay ABV: 13.5% 187 mL",
"description": "Vina Robles Monterey 2017 Chardonnay ABV: 13.5% 187 mL",
"abv": null,
"sub_category_id": 18,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:10:03.077-07:00",
"updated_at": "2018-07-31T00:10:03.223-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 3,
"manufacture_date": null,
"system_sku": "COD0003027",
"sold": 0,
"lightspeed_item_id": 4157,
"quantity": null,
"warehouse_id": 2,
"price": "5.99",
"on_sale": true,
"sale_price": "3.99",
"discount": null,
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
}
]
]
]
I want to serialize this resouces like below
Success.new({
categories: array_serializer.new(categories, each_serializer: Api::V1::CategoriesSerializer),
all_products: array_serializer.new(@products, each_serializer: Api::V1::Products::GetAllProductsSerializer,
}, '', 200)
Is there any way to serialize group by resources ?
ruby-on-rails ruby active-model-serializers
Below is my query which is returning category and its co-responding products.
products = Product
.fetch_products(current_resource_owner)
.where("product_warehouses.on_sale = true")
.group_by(&:category).map { |c, p| [c, p.take(4)] }
Below is the sample response for above query
[
[
{
"id": 1,
"name": "Beer",
"created_at": "2018-05-28T01:30:23.593-07:00",
"updated_at": "2018-08-10T02:15:19.155-07:00",
"image_file_name": "mirassou-pinot-noir-abv-135-750-ml.jpg",
"image_content_type": "image/jpeg",
"image_file_size": 30794,
"image_updated_at": "2018-08-10T02:15:18.521-07:00",
"lightspeed_category_id": null
},
[
{
"id": 3029,
"name": "4 Sons Surf City Pale Ale ABV: 5.9% 12 fl oz 6-Pack",
"description": "4 Sons Surf City Pale Ale ABV: 5.9% 12 fl oz 6-Pack",
"abv": null,
"sub_category_id": 58,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:17:59.410-07:00",
"updated_at": "2018-08-02T22:38:15.998-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 1,
"manufacture_date": null,
"system_sku": "COD0003029",
"sold": 1,
"lightspeed_item_id": 4121,
"quantity": null,
"warehouse_id": 2,
"price": "11.99",
"on_sale": true,
"sale_price": "5.99",
"discount": "50.04",
"is_wishlist": false,
"is_in_cart": true,
"cart_quantity": 1
}
]
],
[
{
"id": 2,
"name": "Spirits",
"created_at": "2018-05-28T01:34:22.975-07:00",
"updated_at": "2018-05-28T01:34:22.975-07:00",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"lightspeed_category_id": null
},
[
{
"id": 3032,
"name": "Nucano Joven Espadín Mezcal ABV: 45% 750 mL",
"description": "Nucano Joven Espadín Mezcal ABV: 45% 750 mL",
"abv": null,
"sub_category_id": 76,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:26:38.227-07:00",
"updated_at": "2018-08-10T02:10:22.804-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "myerss-original-dark-rum-proof-80-375-ml.jpg",
"image_content_type": "image/jpeg",
"image_file_size": 65161,
"image_updated_at": "2018-08-10T02:10:22.250-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003032",
"sold": 0,
"lightspeed_item_id": 4151,
"quantity": "50_ml",
"warehouse_id": 2,
"price": "61.99",
"on_sale": true,
"sale_price": "40.99",
"discount": "33.88",
"is_wishlist": false,
"is_in_cart": true,
"cart_quantity": 1
},
{
"id": 3010,
"name": "Espolón Tequila Reposado ABV: 40% 750 mL",
"description": "Espolón Tequila Reposado ABV: 40% 750 mL",
"abv": null,
"sub_category_id": 32,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-18T23:28:05.801-07:00",
"updated_at": "2018-07-18T23:28:06.078-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003010",
"sold": 0,
"lightspeed_item_id": 4082,
"quantity": "375_ml",
"warehouse_id": 2,
"price": "23.99",
"on_sale": true,
"sale_price": "10.11",
"discount": "57.86",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3023,
"name": "Azunia Silver Tequila 50 mL",
"description": "Azunia Silver Tequila 50 mL",
"abv": null,
"sub_category_id": 33,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-20T05:43:32.984-07:00",
"updated_at": "2018-07-23T01:22:05.127-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "socfo9k2cvjovliqbsyj",
"image_content_type": "image/jpeg",
"image_file_size": 22150,
"image_updated_at": "2018-07-20T05:43:32.614-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003023",
"sold": 0,
"lightspeed_item_id": 1811,
"quantity": "180_ml",
"warehouse_id": 2,
"price": "33.66",
"on_sale": true,
"sale_price": "23.66",
"discount": "29.71",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3020,
"name": "Hornitos Silver Tequila Proof: 80 375 mL",
"description": "Hornitos Silver Tequila Proof: 80 375 mL",
"abv": null,
"sub_category_id": 33,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-20T05:43:15.107-07:00",
"updated_at": "2018-07-20T05:43:15.972-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": "dejbqd9vkzbqiywmj8ye",
"image_content_type": "image/jpeg",
"image_file_size": 57468,
"image_updated_at": "2018-07-20T05:43:13.290-07:00",
"category_id": 2,
"manufacture_date": null,
"system_sku": "COD0003020",
"sold": 0,
"lightspeed_item_id": 1806,
"quantity": "750_ml",
"warehouse_id": 2,
"price": "15.99",
"on_sale": true,
"sale_price": "12.99",
"discount": "18.76",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
}
]
],
[
{
"id": 3,
"name": "Wine",
"created_at": "2018-05-28T01:47:47.497-07:00",
"updated_at": "2018-05-28T01:47:47.497-07:00",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"lightspeed_category_id": null
},
[
{
"id": 3031,
"name": "Vanderpump 2017 Rosé ABV: 13% 750 mL",
"description": "Vanderpump 2017 Rosé ABV: 13% 750 mL",
"abv": null,
"sub_category_id": 98,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:18:02.332-07:00",
"updated_at": "2018-07-31T00:18:02.483-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 3,
"manufacture_date": null,
"system_sku": "COD0003031",
"sold": 0,
"lightspeed_item_id": 4123,
"quantity": null,
"warehouse_id": 2,
"price": "23.99",
"on_sale": true,
"sale_price": "12.99",
"discount": "45.85",
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
},
{
"id": 3027,
"name": "Vina Robles Monterey 2017 Chardonnay ABV: 13.5% 187 mL",
"description": "Vina Robles Monterey 2017 Chardonnay ABV: 13.5% 187 mL",
"abv": null,
"sub_category_id": 18,
"brand_id": 1,
"product_type_id": 1,
"created_at": "2018-07-31T00:10:03.077-07:00",
"updated_at": "2018-07-31T00:10:03.223-07:00",
"region": "USA",
"current_availability": "in_stock",
"image_file_name": null,
"image_content_type": null,
"image_file_size": null,
"image_updated_at": null,
"category_id": 3,
"manufacture_date": null,
"system_sku": "COD0003027",
"sold": 0,
"lightspeed_item_id": 4157,
"quantity": null,
"warehouse_id": 2,
"price": "5.99",
"on_sale": true,
"sale_price": "3.99",
"discount": null,
"is_wishlist": false,
"is_in_cart": false,
"cart_quantity": null
}
]
]
]
I want to serialize this resouces like below
Success.new({
categories: array_serializer.new(categories, each_serializer: Api::V1::CategoriesSerializer),
all_products: array_serializer.new(@products, each_serializer: Api::V1::Products::GetAllProductsSerializer,
}, '', 200)
Is there any way to serialize group by resources ?
ruby-on-rails ruby active-model-serializers
ruby-on-rails ruby active-model-serializers
asked Nov 19 at 11:17
Vishal
2,7641932
2,7641932
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373479%2frails-add-serializer-for-group-by-resources%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown