I
I
Ivan Nesterovich2014-01-20 23:36:18
Ruby on Rails
Ivan Nesterovich, 2014-01-20 23:36:18

How to get all has_and_belongs_to_many links in ruby ​​on rails?

We have a table of goods. Each product can have many categories (has_and_belongs_to_many). Each category can have many properties (has_and_belongs_to_many).
The task is as follows - on the product page, which has the n-th number of categories, display the properties, while excluding repetitions.
The query is something like this:
@product = Product.includes(:categories => :properties).find(1)
as a result @product .categories will be an array of objects, which in turn will contain arrays of property objects that I need. The question is how to get them from there into one array, while excluding repetitions.
Ps. The essence of all these properties is the ability to create a template for the properties of product categories. If we are talking about Laptops, then I create a property template - RAM, percent, screw, etc... if the category is Great, then there is another property template.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
himik, 2014-01-20
@vanderv

@product.categories.map(&:properties).flatten.uniq

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question