Search code examples
ruby-2.3rails-4-2-1

Check all products belong to a category are enabled or disabled?


I have products belongs to a category and each product has status enabled and disabled enum status: [:disabled, :enabled]. On Category page I want to check weather all products in category all enabled or not.

I tried below code which return array with true or false of each product

c = Category.find(1)
c.products.map{|p|p.enabled?}

With raw mysql query it's possible. But as newbie to rails want to know how to get it done using rails code


Solution

  • Following code will do enabled = c.products.size == c.products.enabled.size enabled will return true or false based on all products status enabled or not