Want to get all user's products by category including category that have no product

Huzai

I have two models - parent and child, and parent has_many children, and I have an array of parents and want to retrieve all of the children for all of those parents including parent that have no children. Parent will render as_json and send to react components.

What is the cleanest way to get all categories with current_user's products including categories that have no product associated with them?

I want to view like this:

Category1

  • Product 1
  • Product 2

Category2

  • Product 1
  • Product 2

Category3

  • (empty)

I already try

@categories = Category.includes(:products).where(products: { user_id: current_user.id }) 
@categories.as_json(include: :products)

#Category that has no product is not included

Model

class Product < ApplicationRecord
  belongs_to :user
  belongs_to :category
end

class Category < ApplicationRecord
  has_many :products
end
fongfan999

You can override as_json method

Category.includes(:products).as_json(user_id: current_user.id)

## model/category.rb
def as_json(options = {})
  json = serializable_hash(options)
  json['products'] = products.where(user_id: options[:user_id]).as_json
  json
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get manufacturers of all products in a category from non-product page

How to get all products from current WooCommerce product category?

Get current product category all products parent sku's and display them in array - Woocommerce

Get all WooCommerce products from a category that have specific meta data

Listing a User's Products by Category in Eloquent

Magento - Get all products in the current category

Get all Woocommerce products from current product category term Id in a WP_Query

Get on sale products for Woocommerce product category archives using a Get request

Is there a way to get all the category slug of a woocommerce product

Get all item's in a category?

want to get products group by category in mysql from multiple categories

prestashop get category product

WooCommerce: assign a category to all products

Display All Products by Category with WooCommerce

Using Laravel Eloquent need to get all products according to category id

Get products by multiple category id

Move product category description after displayed category products in Woocommerce

Automatically assign products to a defined product category in WooCommerce

Exclude a product category from Woocommerce related products

Exclude a product category from Related products in WooCommerce

Hide products having a specific product category in Woocommerce

Adding "Sale" product category to products that are on sale in Woocommerce

Display products from specific product category in Woocommerce

Show products from a specific category in the product list

Get all attributes with their terms related to a product category in Woocommerce

Show product-category thumbnails in loop which have n price of products in woocommerce

WooCommerce: function that returns all product ID's in a particular category

woocommerce get product category id

Exclude Woocommerce products in recently viewed products widget from a product category