ruby on rails - How to get values from 3 models and build a structure -


i have 3 models. location, product , stock.

i trying build report around stock each location each product.

so aiming this

location product quantity product b quantity b

and locations

what best way going it?

models

class product < applicationrecord   has_many :stocks, dependent: :destroy   has_many :locations, :through => :stocks end  class location < applicationrecord   has_many :stocks   has_many :products, :through => :stocks end  class stock < applicationrecord   belongs_to :location, optional: true   belongs_to :product, optional: true end 

activerecord allows access associated objects through dot notation. in view, can loop through each of locations , display associated products , stocks.

@locations.each |l|     l.products.each |p|         puts p.name     end     l.stocks.each |s|         puts s.name     end end 

put in table or list format liking.


Comments