I am working on rails 7 with 2 models which are Partner and Package, and I would like to assign the packages to the partner, but they need to be assigned together with their orders.
For example, if I assign package#A and package#B to the partner with 1st and 2nd order respectively, then package#A will be considered as a small package and package#B will be considered as a medium package.
So, I have created PartnerPackage to link them together (with has_many through:) and also added package_order column to contain the package's order.
Question
What is the best way to retrieve all of the packages that belong to a specific partner, and also package_order as well? Thank you so much for your answer!
Partner.rb
class Partner < ApplicationRecord
has_many :partner_packages, dependent: :destroy
has_many :packages, through: :partner_packages
end
Package.rb
class Package < ApplicationRecord
has_many :partner_packages, dependent: :destroy
has_many :packages, through: :partner_packages
end
PartnerPackage.rb
class PartnerPackage < ApplicationRecord
belongs_to :partner
belongs_to :package
validates :package_order, presence: true
end