D
D
dexdev2014-12-17 22:06:20
Ruby on Rails
dexdev, 2014-12-17 22:06:20

How to pull dimensions from a collection of objects in rails?

There are categories, there are sub-categories and there are products, products have a size, each product has its own size, all sizes are different, for example, for refrigerators from 5 to 10 and for microwaves from 0.5 to 3, as on the page with products of a certain sub-category dynamically pull out all the sizes that belong to subcategories, grouped by size for example from 6 to 10?
Where to dig? how to do it?
Can create a size model to associate with subcategories or what?
So that the links were according to this principle
diameter: 6...10 11...40 41...59 60...80 82.5...114 118...152 152.4...203 219...325 330 ...555
Now it looks like this:
Goods

class Item < ActiveRecord::Base
  belongs_to :sub_category, foreign_key: 'category_id'
  has_many :cart_items
  has_many :carts, through: :cart_items
end

Subcategories
class SubCategory < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged

  belongs_to :category
  has_many :items, foreign_key: "category_id"

  	def normalize_friendly_id(input)
    	input.to_s.to_slug.normalize(transliterations: :russian).to_s
  	end
end

Table Items
id | name | category_id | price | size |
1 | briefs | 15 | 2000|6x0.8|
2 | pants c | 15 | 2500|6x0.5|
notch
id | name | category_id | price | size |
1 | briefs | 15 | 2000|6x0.8|
2 | pants c | 15 | 2500|6x0.5|
notch
id | name | category_id | price | size |
1 | cable | 25 | 350 |10x3|
1 | cable| 25 | 350 |10x3.5|
and diameters to them
diameter: 6...10 11...39 40...
140

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Krasnodemsky, 2014-12-17
@AdilA

Item.where(:category_id => your_category_id, :size => 6..10)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question