product.rb
class Product < ApplicationRecord
has_many :product_items
end
product_item.rb
class ProductItem < ApplicationRecord
belongs_to :product
has_many :prices
end
price.rb
class Price < ApplicationRecord
belongs_to :product_item
end
Now price table has a column called otc, it stores one-time cost for the product item and product item can have many prices.
Objective: The product has many product items but I need to find one minimum cost product item(say default_product_item). Need to select product item(along with otc) for a product with minimum otc from price table.
I am using ruby 3.0.1, rails 6.1.3.2, database, Postgres.