I have Product and ProductType
class ProductType < ApplicationRecord
has_many :products
end
class Product < ApplicationRecord
belongs_to :product_type
def type_name
self.product_type.name
end
end
I can use “type_name” in Product index view to show the product type for each product
Code | Name | Type_Name |
---|---|---|
P0001 | Croissant | Viennoiserie |
p0002 | Sacher Torte | Cakes |
P0003 | Brioche pistachio | Viennoiserie |
P0004 | Choux chocolate | Choux |
….. | …………. | ……… |
but I cannot use type_name to have a Product collection ordered by type_name like this
Code | Name | Type_Name |
---|---|---|
p0002 | Sacher Torte | Cakes |
P0004 | Choux chocolate | Choux |
P0001 | Croissant | Viennoiserie |
P0003 | Brioche pistachio | Viennoiserie |
….. | …………. | ……… |
I tried to search both S.O. and google in general but it seems very few have this quite common problem, and I tried the proposed solutions (using scope or Arel) but with no success